admin-bar
1 year ago
client-migration
6 months ago
compatibility
1 year ago
customizer
1 year ago
freemius
8 months ago
menu-icons
1 year ago
metabox
1 year ago
onboarding
2 weeks ago
panel
9 months ago
post-settings
2 months ago
preloader
1 year ago
shortcodes
1 year ago
themepanel
1 year ago
widgets
8 months ago
wizard
3 years ago
adobe-font.php
1 year ago
custom-code.php
8 months ago
dashboard.php
1 year ago
image-resizer.php
1 year ago
jshrink.php
3 years ago
mautic.php
2 months ago
ocean-extra-strings.php
3 years ago
plugins-tab.php
1 year ago
update-message.php
1 year ago
utils.php
1 year ago
walker.php
4 years ago
plugins-tab.php
210 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OceanWP Plugins Tab |
| 4 | * |
| 5 | * @package OceanWP WordPress theme |
| 6 | */ |
| 7 | |
| 8 | // Exit if accessed directly. |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Class OceanWP_Plugins_Tab |
| 15 | * |
| 16 | * Adds a custom tab to the plugin install screen to display OceanWP plugins. |
| 17 | */ |
| 18 | class OceanWP_Plugins_Tab { |
| 19 | |
| 20 | /** |
| 21 | * OceanWP_Plugins_Tab constructor. |
| 22 | * |
| 23 | * Hooks the methods to the appropriate actions and filters. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | add_filter( 'install_plugins_tabs', array( $this, 'add_oceanwp_plugin_tab' ) ); |
| 27 | add_action( 'install_plugins_oceanwp_plugins_tab', array( $this, 'display_oceanwp_plugins_tab_content' ) ); |
| 28 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 29 | add_action( 'wp_ajax_oceanwp_install_plugin', array( $this, 'ajax_install_plugin' ) ); |
| 30 | add_action( 'wp_ajax_oceanwp_activate_plugin', array( $this, 'ajax_activate_plugin' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Enqueues the necessary scripts for handling AJAX plugin installation. |
| 35 | */ |
| 36 | public function enqueue_scripts( $hook_suffix ) { |
| 37 | // Only enqueue the scripts on the plugin installation page and our custom tab. |
| 38 | if ( 'plugin-install.php' === $hook_suffix && isset( $_GET['tab'] ) && 'oceanwp_plugins_tab' === $_GET['tab'] ) { |
| 39 | wp_enqueue_script( 'plugin-install' ); |
| 40 | wp_enqueue_script( 'updates' ); |
| 41 | wp_enqueue_script( 'oceanwp-plugin-install', plugin_dir_url( __FILE__ ) . '../assets/js/oceanwp-plugin-install.js', array( 'jquery' ), OE_VERSION, true ); |
| 42 | |
| 43 | wp_localize_script( |
| 44 | 'oceanwp-plugin-install', |
| 45 | 'oceanwpPluginInstall', |
| 46 | array( |
| 47 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 48 | 'nonce' => wp_create_nonce( 'plugin_install_nonce' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Adds a custom tab to the plugin install screen. |
| 56 | * |
| 57 | * @param array $tabs The existing tabs. |
| 58 | * @return array The modified tabs. |
| 59 | */ |
| 60 | public function add_oceanwp_plugin_tab( $tabs ) { |
| 61 | // Check if the current user has the capability to install plugins. |
| 62 | if ( apply_filters( 'oceanwp_show_plugin_tab', current_user_can( 'install_plugins' ) ) ) { |
| 63 | $tabs['oceanwp_plugins_tab'] = __( 'For OceanWP', 'ocean-extra' ); // Add new tab. |
| 64 | } |
| 65 | return $tabs; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Displays the content for the custom OceanWP plugins tab. |
| 70 | */ |
| 71 | public function display_oceanwp_plugins_tab_content() { |
| 72 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 73 | wp_die( __( 'You do not have sufficient permissions to access this page.', 'ocean-extra' ) ); |
| 74 | } |
| 75 | |
| 76 | ?> |
| 77 | <div class="wrap"> |
| 78 | <h2><?php _e( 'For OceanWP', 'ocean-extra' ); ?></h2> |
| 79 | <div id="oceanwp-plugin-list"> |
| 80 | <?php |
| 81 | // Query Plugins by Author. |
| 82 | $api = plugins_api( |
| 83 | 'query_plugins', |
| 84 | array( |
| 85 | 'author' => 'oceanwp', |
| 86 | 'per_page' => 20, |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | if ( is_wp_error( $api ) ) { |
| 91 | echo '<div class="error"><p>' . $api->get_error_message() . '</p></div>'; |
| 92 | } else { |
| 93 | $this->ocean_display_plugins_table( $api->plugins ); |
| 94 | } |
| 95 | ?> |
| 96 | </div> |
| 97 | </div> |
| 98 | <?php |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Displays the plugins using the default WordPress layout. |
| 104 | * |
| 105 | * @param array $plugins The plugins to display. |
| 106 | */ |
| 107 | private function ocean_display_plugins_table( $plugins ) { |
| 108 | global $wp_list_table; |
| 109 | |
| 110 | if ( ! class_exists( 'WP_Plugin_Install_List_Table' ) ) { |
| 111 | require_once ABSPATH . 'wp-admin/includes/class-wp-plugin-install-list-table.php'; |
| 112 | } |
| 113 | |
| 114 | $wp_list_table = new WP_Plugin_Install_List_Table( |
| 115 | array( |
| 116 | 'screen' => 'plugin-install', |
| 117 | ) |
| 118 | ); |
| 119 | |
| 120 | $wp_list_table->items = $plugins; |
| 121 | $wp_list_table->display(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Handles the AJAX request to install a plugin. |
| 126 | */ |
| 127 | public function ajax_install_plugin() { |
| 128 | check_ajax_referer( 'plugin_install_nonce', '_ajax_nonce' ); |
| 129 | |
| 130 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 131 | wp_send_json_error( __( 'You do not have sufficient permissions to install plugins.', 'ocean-extra' ) ); |
| 132 | } |
| 133 | |
| 134 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 135 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 136 | |
| 137 | $slug = sanitize_text_field( $_POST['slug'] ); |
| 138 | $api = plugins_api( |
| 139 | 'plugin_information', |
| 140 | array( |
| 141 | 'slug' => $slug, |
| 142 | 'fields' => array( |
| 143 | 'sections' => false, |
| 144 | ), |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | if ( is_wp_error( $api ) ) { |
| 149 | wp_send_json_error( $api->get_error_message() ); |
| 150 | } |
| 151 | |
| 152 | $skin = new Automatic_Upgrader_Skin(); |
| 153 | $upgrader = new Plugin_Upgrader( $skin ); |
| 154 | $result = $upgrader->install( $api->download_link ); |
| 155 | |
| 156 | if ( is_wp_error( $result ) ) { |
| 157 | wp_send_json_error( $result->get_error_message() ); |
| 158 | } |
| 159 | |
| 160 | wp_send_json_success(); |
| 161 | } |
| 162 | /** |
| 163 | * Handles the AJAX request to activate a plugin. |
| 164 | */ |
| 165 | public function ajax_activate_plugin() { |
| 166 | check_ajax_referer( 'plugin_install_nonce', '_ajax_nonce' ); |
| 167 | |
| 168 | if ( ! current_user_can( 'activate_plugins' ) ) { |
| 169 | wp_send_json_error( __( 'You do not have sufficient permissions to activate plugins.', 'ocean-extra' ) ); |
| 170 | } |
| 171 | |
| 172 | $slug = sanitize_text_field( $_POST['slug'] ); |
| 173 | $plugin_file = $this->get_plugin_file_path( $slug ); |
| 174 | |
| 175 | if ( ! $plugin_file || ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) { |
| 176 | wp_send_json_error( __( 'Plugin file does not exist.', 'ocean-extra' ) ); |
| 177 | } |
| 178 | |
| 179 | $result = activate_plugin( $plugin_file ); |
| 180 | |
| 181 | if ( is_wp_error( $result ) ) { |
| 182 | wp_send_json_error( $result->get_error_message() ); |
| 183 | } |
| 184 | |
| 185 | wp_send_json_success(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get the plugin file path based on the plugin slug. |
| 190 | * |
| 191 | * @param string $slug The plugin slug. |
| 192 | * @return string|false The plugin file path or false if not found. |
| 193 | */ |
| 194 | private function get_plugin_file_path( $slug ) { |
| 195 | $plugins = get_plugins(); |
| 196 | |
| 197 | foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 198 | if ( strpos( $plugin_file, $slug . '/' ) !== false || strpos( $plugin_file, $slug . '.php' ) !== false ) { |
| 199 | return $plugin_file; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Initialize the class |
| 208 | new OceanWP_Plugins_Tab(); |
| 209 | |
| 210 |