| @@ -1,15 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace PXLBSAdminify\Inc\Classes; | |
| 3 | +namespace WPAdminify\Inc\Classes; | |
| 4 | 4 | |
| 5 | -use PXLBSAdminify\Libs\Addons; | |
| 5 | +use WPAdminify\Libs\Addons; | |
| 6 | 6 | |
| 7 | -// no direct access allowed | |
| 8 | -if (!defined('ABSPATH')) { | |
| 9 | - exit; | |
| 10 | -} | |
| 11 | - | |
| 12 | 7 | if (!class_exists('Addons_Plugins')) { |
| 13 | 8 | /** |
| 14 | 9 | * Addons Plugins class |
| 15 | 10 | * |
| @@ -17,9 +12,9 @@ | ||
| 17 | 12 | */ |
| 18 | 13 | class Addons_Plugins extends Addons |
| 19 | 14 | { |
| 20 | 15 | |
| 21 | - private $transient_key = 'pxlbsadminify_addon_plugins_data'; | |
| 16 | + private $transient_key = 'wp_adminify_addon_plugins_data'; | |
| 22 | 17 | |
| 23 | 18 | /** |
| 24 | 19 | * Constructor method |
| 25 | 20 | */ |
| @@ -29,10 +24,9 @@ | ||
| 29 | 24 | parent::__construct($this->menu_order); |
| 30 | 25 | add_action('admin_enqueue_scripts', [$this, 'add_addons_thickbox']); |
| 31 | 26 | } |
| 32 | 27 | |
| 33 | - public function add_addons_thickbox() | |
| 34 | - { | |
| 28 | + public function add_addons_thickbox () { | |
| 35 | 29 | add_thickbox(); |
| 36 | 30 | } |
| 37 | 31 | |
| 38 | 32 | /** |
| @@ -75,54 +69,65 @@ | ||
| 75 | 69 | // Return plugins data from cache if found |
| 76 | 70 | $plugins_data = get_transient($this->transient_key); |
| 77 | 71 | if (!empty($plugins_data)) return $plugins_data; |
| 78 | 72 | |
| 79 | - // Read the add-ons catalogue from the copy bundled with the plugin | |
| 80 | - // (no remote request). | |
| 81 | - $catalogue_file = PXLBSADMINIFY_PATH . 'Inc/data/adminify-plugins.json'; | |
| 73 | + // URL of the raw file in the GitHub repository | |
| 74 | + $file_url = 'https://raw.githubusercontent.com/jeweltheme/adminify-addons-assets/main/adminify-plugins.json'; | |
| 82 | 75 | |
| 83 | - if (!is_readable($catalogue_file)) { | |
| 84 | - return array(); | |
| 85 | - } | |
| 76 | + // Make the request to fetch the file contents | |
| 77 | + $response = wp_remote_get($file_url); | |
| 86 | 78 | |
| 87 | - $data_array = wp_json_file_decode($catalogue_file, array('associative' => true)); | |
| 88 | 79 | |
| 89 | - if (empty($data_array) || !is_array($data_array)) { | |
| 90 | - return array(); | |
| 91 | - } | |
| 80 | + // Check if the request was successful | |
| 81 | + if (!is_wp_error($response) && $response['response']['code'] === 200) { | |
| 82 | + // Get the body of the response (file contents) | |
| 83 | + $file_contents = wp_remote_retrieve_body($response); | |
| 92 | 84 | |
| 93 | - $plugins_data = []; | |
| 85 | + // Decode the JSON data into an array | |
| 86 | + $data_array = json_decode($file_contents, true); | |
| 94 | 87 | |
| 95 | - foreach ($data_array as $plugin_data) { | |
| 88 | + // Check if JSON decoding was successful | |
| 89 | + if (!empty($data_array)) { | |
| 90 | + // Now you have the data in an array format, you can use it as needed | |
| 91 | + $plugins_data = []; | |
| 96 | 92 | |
| 97 | - $plugins_data[$plugin_data['slug']] = $plugin_data; | |
| 93 | + foreach ($data_array as $plugin_data) { | |
| 98 | 94 | |
| 99 | - if (str_contains($plugin_data['download_link'], 'downloads.wordpress.org')) { | |
| 95 | + $plugins_data[ $plugin_data['slug'] ] = $plugin_data; | |
| 100 | 96 | |
| 101 | - require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); | |
| 97 | + if ( str_contains( $plugin_data['download_link'], 'downloads.wordpress.org' ) ) { | |
| 102 | 98 | |
| 103 | - $plugin_info = \plugins_api('plugin_information', array('slug' => $plugin_data['slug'])); | |
| 104 | - if (is_wp_error($plugin_info)) { | |
| 105 | - unset($plugins_data[$plugin_data['slug']]); | |
| 106 | - continue; | |
| 107 | - } | |
| 108 | - $plugins_data[$plugin_data['slug']]['version'] = $plugin_info->version; | |
| 109 | - } else { | |
| 99 | + require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); | |
| 110 | 100 | |
| 111 | - if (! isset($plugin_data['version'])) { | |
| 112 | - unset($plugins_data[$plugin_data['slug']]); | |
| 113 | - continue; | |
| 101 | + $plugin_info = \plugins_api( 'plugin_information', array( 'slug' => $plugin_data['slug'] ) ); | |
| 102 | + $plugins_data[ $plugin_data['slug'] ]['version'] = $plugin_info->version; | |
| 103 | + | |
| 104 | + } else { | |
| 105 | + | |
| 106 | + if ( ! isset( $plugin_data['version'] ) ) { | |
| 107 | + unset( $plugins_data[ $plugin_data['slug'] ] ); | |
| 108 | + continue; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $download_link = add_query_arg( 'version', $plugin_data['version'], $plugin_data['download_link'] ); | |
| 112 | + $plugins_data[ $plugin_data['slug'] ]['download_link'] = $download_link; | |
| 113 | + | |
| 114 | + } | |
| 115 | + | |
| 114 | 116 | } |
| 115 | 117 | |
| 116 | - $download_link = add_query_arg('version', $plugin_data['version'], $plugin_data['download_link']); | |
| 117 | - $plugins_data[$plugin_data['slug']]['download_link'] = $download_link; | |
| 118 | + // Get plugins data from API and cache it | |
| 119 | + $status = set_transient($this->transient_key, $plugins_data, DAY_IN_SECONDS / 2); | |
| 120 | + if (!$status) return []; | |
| 121 | + return $plugins_data; | |
| 122 | + } else { | |
| 123 | + // JSON decoding failed | |
| 124 | + echo esc_html__('Error while data retrieval.', 'adminify'); | |
| 118 | 125 | } |
| 126 | + } else { | |
| 127 | + // Request failed | |
| 128 | + echo esc_html__('Failed to retrieve file from Source.', 'adminify'); | |
| 119 | 129 | } |
| 120 | - | |
| 121 | - // Cache the processed catalogue. | |
| 122 | - set_transient($this->transient_key, $plugins_data, DAY_IN_SECONDS / 2); | |
| 123 | - | |
| 124 | - return $plugins_data; | |
| 125 | 130 | } |
| 126 | 131 | |
| 127 | 132 | |
| 128 | 133 | /** |
| @@ -131,14 +136,9 @@ | ||
| 131 | 136 | * @author Jewel Theme <support@jeweltheme.com> |
| 132 | 137 | */ |
| 133 | 138 | public function plugins_list() |
| 134 | 139 | { |
| 135 | - // Do not hit the remote source on every admin load. At construction | |
| 136 | - // we only return the cached catalogue; the live fetch happens on the | |
| 137 | - // Add-ons page (get_addons_plugins_list) and on addon install, both | |
| 138 | - // of which are explicit user actions. | |
| 139 | - $cached = get_transient( $this->transient_key ); | |
| 140 | - return ! empty( $cached ) ? $cached : array(); | |
| 140 | + return $this->get_adminify_plugins_lists(); | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
| 144 | 144 | * Admin submenu |
| @@ -144,13 +144,13 @@ | ||
| 144 | 144 | * Admin submenu |
| 145 | 145 | */ |
| 146 | 146 | public function admin_menu() |
| 147 | 147 | { |
| 148 | - $submenu_position = apply_filters('pxlbsadminify_submenu_position', 50); | |
| 148 | + $submenu_position = apply_filters('jltwp_adminify_submenu_position', 50); | |
| 149 | 149 | add_submenu_page( |
| 150 | 150 | 'wp-adminify-settings', // Ex. wp-adminify-settings / edit.php?post_type=page . |
| 151 | 151 | __('Addons', 'adminify'), |
| 152 | - sprintf('<span class="adminify-addons-text">%s</span>', __('Addons', 'adminify')), | |
| 152 | + sprintf( '<span class="adminify-addons-text">%s</span>', __( 'Addons', 'adminify' ) ), | |
| 153 | 153 | 'manage_options', |
| 154 | 154 | 'wp-adminify-addons-plugins', |
| 155 | 155 | array($this, 'render_addons_plugins'), |
| 156 | 156 | $submenu_position |