| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Classes; |
| 4 |
|
| 5 |
use WPAdminify\Libs\Addons; |
| 6 |
|
| 7 |
if (!class_exists('Addons_Plugins')) { |
| 8 |
/** |
| 9 |
* Addons Plugins class |
| 10 |
* |
| 11 |
* Jewel Theme <support@jeweltheme.com> |
| 12 |
*/ |
| 13 |
class Addons_Plugins extends Addons |
| 14 |
{ |
| 15 |
|
| 16 |
private $transient_key = 'wp_adminify_addon_plugins_data'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor method |
| 20 |
*/ |
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->menu_order = 99; // for submenu order value should be more than 10 . |
| 24 |
parent::__construct($this->menu_order); |
| 25 |
add_action('admin_enqueue_scripts', [$this, 'add_addons_thickbox']); |
| 26 |
} |
| 27 |
|
| 28 |
public function add_addons_thickbox() |
| 29 |
{ |
| 30 |
add_thickbox(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Menu list |
| 35 |
*/ |
| 36 |
public function menu_items() |
| 37 |
{ |
| 38 |
return array( |
| 39 |
array( |
| 40 |
'key' => 'all', |
| 41 |
'label' => 'All', |
| 42 |
), |
| 43 |
array( |
| 44 |
'key' => 'featured', // key should be used as category to the plugin list. |
| 45 |
'label' => 'Featured Item', |
| 46 |
), |
| 47 |
array( |
| 48 |
'key' => 'popular', |
| 49 |
'label' => 'Popular', |
| 50 |
), |
| 51 |
array( |
| 52 |
'key' => 'favorites', |
| 53 |
'label' => 'Favorites', |
| 54 |
), |
| 55 |
array( |
| 56 |
'key' => 'recommended', |
| 57 |
'label' => 'Recommended', |
| 58 |
), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get data from github |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function get_adminify_plugins_lists() |
| 68 |
{ |
| 69 |
|
| 70 |
// Return plugins data from cache if found |
| 71 |
$plugins_data = get_transient($this->transient_key); |
| 72 |
if (!empty($plugins_data)) return $plugins_data; |
| 73 |
|
| 74 |
// URL of the raw file in the GitHub repository |
| 75 |
$file_url = 'https://raw.githubusercontent.com/jeweltheme/adminify-addons-assets/main/adminify-plugins.json'; |
| 76 |
|
| 77 |
// Make the request to fetch the file contents |
| 78 |
$response = wp_remote_get($file_url); |
| 79 |
|
| 80 |
|
| 81 |
// Check if the request was successful |
| 82 |
if (!is_wp_error($response) && $response['response']['code'] === 200) { |
| 83 |
// Get the body of the response (file contents) |
| 84 |
$file_contents = wp_remote_retrieve_body($response); |
| 85 |
|
| 86 |
// Decode the JSON data into an array |
| 87 |
$data_array = json_decode($file_contents, true); |
| 88 |
|
| 89 |
// Check if JSON decoding was successful |
| 90 |
if (!empty($data_array)) { |
| 91 |
// Now you have the data in an array format, you can use it as needed |
| 92 |
$plugins_data = []; |
| 93 |
|
| 94 |
foreach ($data_array as $plugin_data) { |
| 95 |
|
| 96 |
$plugins_data[$plugin_data['slug']] = $plugin_data; |
| 97 |
|
| 98 |
if (str_contains($plugin_data['download_link'], 'downloads.wordpress.org')) { |
| 99 |
|
| 100 |
require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); |
| 101 |
|
| 102 |
$plugin_info = \plugins_api('plugin_information', array('slug' => $plugin_data['slug'])); |
| 103 |
if (is_wp_error($plugin_info)) { |
| 104 |
unset($plugins_data[$plugin_data['slug']]); |
| 105 |
continue; |
| 106 |
} |
| 107 |
$plugins_data[$plugin_data['slug']]['version'] = $plugin_info->version; |
| 108 |
} else { |
| 109 |
|
| 110 |
if (! isset($plugin_data['version'])) { |
| 111 |
unset($plugins_data[$plugin_data['slug']]); |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$download_link = add_query_arg('version', $plugin_data['version'], $plugin_data['download_link']); |
| 116 |
$plugins_data[$plugin_data['slug']]['download_link'] = $download_link; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Get plugins data from API and cache it |
| 121 |
$status = set_transient($this->transient_key, $plugins_data, DAY_IN_SECONDS / 2); |
| 122 |
if (!$status) return []; |
| 123 |
return $plugins_data; |
| 124 |
} else { |
| 125 |
// JSON decoding failed |
| 126 |
echo esc_html__('Error while data retrieval.', 'adminify'); |
| 127 |
} |
| 128 |
} else { |
| 129 |
// Request failed |
| 130 |
echo esc_html__('Failed to retrieve file from Source.', 'adminify'); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Plugins List |
| 137 |
* |
| 138 |
* @author Jewel Theme <support@jeweltheme.com> |
| 139 |
*/ |
| 140 |
public function plugins_list() |
| 141 |
{ |
| 142 |
return $this->get_adminify_plugins_lists(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Admin submenu |
| 147 |
*/ |
| 148 |
public function admin_menu() |
| 149 |
{ |
| 150 |
$submenu_position = apply_filters('jltwp_adminify_submenu_position', 50); |
| 151 |
add_submenu_page( |
| 152 |
'wp-adminify-settings', // Ex. wp-adminify-settings / edit.php?post_type=page . |
| 153 |
__('Addons', 'adminify'), |
| 154 |
sprintf('<span class="adminify-addons-text">%s</span>', __('Addons', 'adminify')), |
| 155 |
'manage_options', |
| 156 |
'wp-adminify-addons-plugins', |
| 157 |
array($this, 'render_addons_plugins'), |
| 158 |
$submenu_position |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|