diff --git a/tgm-plugin-activation/auto-install.php b/tgm-plugin-activation/auto-install.php new file mode 100644 index 00000000..29602f1b --- /dev/null +++ b/tgm-plugin-activation/auto-install.php @@ -0,0 +1,218 @@ + + * @copyright Copyright (c) 2011, Thomas Griffin + * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License + * @link https://github.com/thomasgriffin/TGM-Plugin-Activation + * + */ + + +/* Copyright 2011 Thomas Griffin (email : thomas@thomasgriffinmedia.com) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 3, as + published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + +if ( ! is_admin() ) // If we are not in the admin section, bail out + return; + + +class TGM_Plugin_Activation { + + var $instance; // DO NOT MODIFY THIS + + // All of the variables below can be modified to suit your specific plugin. + + var $plugin = 'tgm-example-plugin/tgm-example-plugin.php'; // The main plugin file (you must include the plugin folder) + var $plugin_name = 'TGM Example Plugin'; // The name of your plugin + var $zip = 'tgm-example-plugin.zip'; // The name of the zip file that contains your plugin + var $menu = 'tgm-example-plugin'; // The name of your menu page (will appear in the URL as themes.php?page=tgm-example-plugin) + var $nonce = 'tgm_plugins'; // You can set your own nonce name if you would + + + /** + * Adds actions for class methods. + * + * Adds two new methods for the class: admin_menu and admin_notices. + * admin_notices handles the nag; admin_menu handles the rest. + * + * @since 1.0.0 + * + * @access public + */ + public function __construct() { + + $this->instance =& $this; + + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); + add_action( 'admin_notices', array( $this, 'admin_notices' ) ); + + } + + + /** + * Adds submenu page under 'Appearance' tab. + * + * This method adds the submenu page letting users know that a required plugin needs to be installed. + * This page disappears once the plugin has been installed and activated. + * + * @since 1.0.0 + * + * @access public + */ + public function admin_menu() { + + if ( is_plugin_active( $this->plugin ) ) // No need to output the page if our plugin is already installed and activated + return; + + add_submenu_page( 'themes.php', __( 'Install Required Plugins', 'tgmpa' ), __( 'Install Plugins', 'tgmpa' ), 'edit_theme_options', $this->menu, array( $this, 'tgm_install_plugins_page' ) ); + + } + + + /** + * Outputs plugin installation form. + * + * This method is the callback for the admin_menu method function. + * This displays the admin page and form area where the user can select to install and activate the plugin. + * + * @since 1.0.0 + * + * @access public + */ + public function tgm_install_plugins_page() { + + if ( $this->tgm_do_plugin_install() ) // No need to output the form if tgm_do_plugin_install has been successfully run + return; + + ?> +
+

+

plugin_name . ' plugin is required for this theme. Click on the big blue button below to install and activate ' . $this->plugin_name . '!', 'tgmpa' ); ?>

+
+ nonce ); + echo ""; ?> +
+
+ plugin ) ) // Bail out if our plugin is already activated + return false; + + check_admin_referer( $this->nonce ); + + $fields = array( 'tgm_go' ); + $method = ''; // Leave blank so WP_Filesystem can populate it as necessary + + if ( isset( $_POST['tgm_go'] ) ) { // Don't do anything if the form has not been submitted + + $url = wp_nonce_url( 'themes.php?page=' . $this->menu . '', $this->nonce ); // Make sure we are coming from the right page + if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, $fields ) ) ) + return true; + + if ( ! WP_Filesystem( $creds ) ) { + + request_filesystem_credentials( $url, $method, false, false, $fields ); // Setup WP_Filesystem + return true; + + } + + global $wp_filesystem; // Introduce global $wp_filesystem to use WP_Filesystem class methods + + $source = get_stylesheet_directory() . '/lib/tgm-plugin-activation/plugins/' . $this->zip; // The source zip file + $destination = $wp_filesystem->wp_plugins_dir(); // The destination for where we want our plugin installed (wp-content/plugins) + + $install = unzip_file( $source, $destination ); // Use WP_Filesystem to unzip file to our destination directory (wp-content/plugins) + + if ( is_wp_error ( $install ) ) { // Spit out an error message if the installation fails + $install_error = $install->get_error_message(); + echo '

' . $install_error . '

'; + return false; + } + + if ( $wp_filesystem->is_dir( $destination ) && ! is_plugin_active( $this->plugin ) ) + $activate = activate_plugin( $this->plugin, '', '' ); // Activate our plugin if it exists + + if ( is_wp_error( $activate ) ) { // Spit out an error message if the activation fails + $activate_error = $activate->get_error_message(); + echo '

' . $activate_error . '

'; + return false; + } + + } + + if ( is_plugin_active( $this->plugin ) ) { // Display a message upon successful activation of the plugin + + printf( '

Congratulations!

Congratulations! ' . $this->plugin_name . ' has been successfully installed, activated and is ready for use. Return to the dashboard.

', admin_url(), 'tgmpa' ); + + } + + return true; + + } + + + /** + * Display required notice nag. + * + * Outputs a message telling users that a specific plugin is required for their theme. + * Displays a link to the form page where users can install and activate the plugin. + * + * @since 1.0.0 + * + * @access public + * @param string $output + * @return string HTML markup + */ + public function admin_notices( $output ) { + + if ( ! is_plugin_active( $this->plugin ) && ! isset( $_POST['tgm_go'] ) ) { + + $message = sprintf( __( 'This theme requires the ' . $this->plugin_name . ' plugin. Click here to begin the installation process. You may be asked for FTP credentials based on your server setup.', 'tgmpa' ), admin_url( 'themes.php?page=' . $this->menu . '' ) ); + $output = printf( '

%1$s

', $message ); + + } + + } + +} + +new TGM_Plugin_Activation(); // Instantiate a new instance of the class \ No newline at end of file diff --git a/tgm-plugin-activation/plugins/tgm-example-plugin.zip b/tgm-plugin-activation/plugins/tgm-example-plugin.zip new file mode 100644 index 00000000..4b52df50 Binary files /dev/null and b/tgm-plugin-activation/plugins/tgm-example-plugin.zip differ