theme_name = 'bastet'; if ( class_exists( 'TGM_Plugin_Activation' ) && isset( $GLOBALS['bastet'] ) ) { add_action( 'init', array( $this, 'get_tgmpa_instanse' ), 30 ); add_action( 'init', array( $this, 'set_tgmpa_url' ), 40 ); } add_filter( 'tgmpa_load', array($this, 'tgmpa_load'), 20, 2); add_action( 'wp_ajax_bs_handle_plugins', array($this, 'ajax_plugins')); add_action( 'admin_enqueue_scripts', array($this, 'plugin_sctipts')); } public function plugin_active( $plugin ) { return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin ); } public function _get_plugins() { $instance = call_user_func( array( get_class( $GLOBALS['bastet'] ), 'get_instance' ) ); $plugins = array( 'all' => array(), // Meaning: all plugins which still have open actions. 'install' => array(), 'update' => array(), 'activate' => array(), ); foreach ( $instance->plugins as $slug => $plugin ) { if ( $this->plugin_active( $slug ) && false === $instance->does_plugin_have_update( $slug ) ) { // No need to display plugins if they are installed, up-to-date and active. continue; } else { $plugins['all'][ $slug ] = $plugin; if ( ! $instance->is_plugin_installed( $slug ) ) { $plugins['install'][ $slug ] = $plugin; } else { if ( false !== $instance->does_plugin_have_update( $slug ) ) { $plugins['update'][ $slug ] = $plugin; } if ( $instance->can_plugin_activate( $slug ) ) { $plugins['activate'][ $slug ] = $plugin; } } } } return $plugins; } /** * Register and enqueue class assets */ public function plugin_sctipts(){ wp_enqueue_script( 'bs-plugin-manager', get_template_directory_uri().'/setup/assets/js/plugin-manager.js', array( 'jquery' ), false, false ); wp_localize_script( 'bs-plugin-manager', 'envato_setup_params', array( 'tgm_plugin_nonce' => array( 'update' => wp_create_nonce( 'tgmpa-update' ), 'install' => wp_create_nonce( 'tgmpa-install' ), ), 'tgm_bulk_url' => admin_url( $this->tgmpa_url ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'wpnonce' => wp_create_nonce( 'envato_setup_nonce' ), 'verify_text' => esc_html__( '...verifying', 'bastet' ), ) ); } public function ajax_plugins() { if ( ! check_ajax_referer( 'envato_setup_nonce', 'wpnonce' ) || empty( $_POST['slug'] ) ) { wp_send_json_error( array( 'error' => 1, 'message' => esc_html__( 'No Slug Found', 'bastet' ) ) ); } $json = array(); // send back some json we use to hit up TGM $plugins = $this->_get_plugins(); // what are we doing with this plugin? foreach ( $plugins['activate'] as $slug => $plugin ) { if ( $_POST['slug'] == $slug ) { $json = array( 'url' => admin_url( $this->tgmpa_url ), 'plugin' => array( $slug ), 'tgmpa-page' => $this->tgmpa_menu_slug, 'plugin_status' => 'all', '_wpnonce' => wp_create_nonce( 'bulk-plugins' ), 'action' => 'tgmpa-bulk-activate', 'action2' => - 1, 'message' => esc_html__( 'Activating Plugin', 'bastet' ), ); break; } } foreach ( $plugins['update'] as $slug => $plugin ) { if ( $_POST['slug'] == $slug ) { $json = array( 'url' => admin_url( $this->tgmpa_url ), 'plugin' => array( $slug ), 'tgmpa-page' => $this->tgmpa_menu_slug, 'plugin_status' => 'all', '_wpnonce' => wp_create_nonce( 'bulk-plugins' ), 'action' => 'tgmpa-bulk-update', 'action2' => - 1, 'message' => esc_html__( 'Updating Plugin', 'bastet' ), ); break; } } foreach ( $plugins['install'] as $slug => $plugin ) { if ( $_POST['slug'] == $slug ) { $json = array( 'url' => admin_url( $this->tgmpa_url ), 'plugin' => array( $slug ), 'tgmpa-page' => $this->tgmpa_menu_slug, 'plugin_status' => 'all', '_wpnonce' => wp_create_nonce( 'bulk-plugins' ), 'action' => 'tgmpa-bulk-install', 'action2' => - 1, 'message' => esc_html__( 'Installing Plugin', 'bastet' ), ); break; } } if ( $json ) { $json['hash'] = md5( serialize( $json ) ); // used for checking if duplicates happen, move to next plugin wp_send_json( $json ); } else { //wp_send_json( $json ); wp_send_json( array( 'done' => 1, 'message' => esc_html__( 'Success', 'bastet' ) ) ); } exit; } /** * Page setup */ public function envato_setup_default_plugins() { tgmpa_load_bulk_installer(); // install plugins with TGM. if ( ! class_exists( 'TGM_Plugin_Activation' ) || ! isset( $GLOBALS['bastet'] ) ) { die( 'Failed to find TGM' ); } $url = wp_nonce_url( add_query_arg( array( 'plugins' => 'go' ) ), 'envato-setup' ); $plugins = $this->_get_plugins(); // copied from TGM $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) { return true; // Stop the normal page form from displaying, credential request form will be shown. } // Now we have some credentials, setup WP_Filesystem. if ( ! WP_Filesystem( $creds ) ) { // Our credentials were no good, ask the user for them again. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields ); return true; } /* If we arrive here, we have the filesystem */ ?>

_get_plugins(); if ( count( $plugins['all'] ) ) { ?>

' . esc_html__( 'Good news! All plugins are already installed and up to date. Please continue.', 'bastet' ) . '

'; } ?>

tgmpa_instance = call_user_func( array( get_class( $GLOBALS['bastet'] ), 'get_instance' ) ); } /** * Let TGMPA work on ajax also * * @access public * @since 1.0.0 */ public function tgmpa_load( $status ) { return is_admin() && current_user_can( 'install_themes' ); } /** * Update $tgmpa_menu_slug and $tgmpa_parent_slug from TGMPA instance * * @access public * @since 1.0.0 */ public function set_tgmpa_url() { $this->tgmpa_menu_slug = ( property_exists( $this->tgmpa_instance, 'menu' ) ) ? $this->tgmpa_instance->menu : $this->tgmpa_menu_slug; $this->tgmpa_menu_slug = apply_filters( $this->theme_name . '_theme_setup_wizard_tgmpa_menu_slug', $this->tgmpa_menu_slug ); $tgmpa_parent_slug = ( property_exists( $this->tgmpa_instance, 'parent_slug' ) && $this->tgmpa_instance->parent_slug !== 'themes.php' ) ? 'admin.php' : 'themes.php'; $this->tgmpa_url = apply_filters( $this->theme_name . '_theme_setup_wizard_tgmpa_url', $tgmpa_parent_slug . '?page=' . $this->tgmpa_menu_slug ); } } ?>