| 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 |
add_thickbox(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Menu list |
| 34 |
*/ |
| 35 |
public function menu_items() |
| 36 |
{ |
| 37 |
return array( |
| 38 |
array( |
| 39 |
'key' => 'all', |
| 40 |
'label' => 'All', |
| 41 |
), |
| 42 |
array( |
| 43 |
'key' => 'featured', // key should be used as category to the plugin list. |
| 44 |
'label' => 'Featured Item', |
| 45 |
), |
| 46 |
array( |
| 47 |
'key' => 'popular', |
| 48 |
'label' => 'Popular', |
| 49 |
), |
| 50 |
array( |
| 51 |
'key' => 'favorites', |
| 52 |
'label' => 'Favorites', |
| 53 |
), |
| 54 |
array( |
| 55 |
'key' => 'recommended', |
| 56 |
'label' => 'Recommended', |
| 57 |
), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get data from github |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function get_adminify_plugins_lists() |
| 67 |
{ |
| 68 |
|
| 69 |
// Return plugins data from cache if found |
| 70 |
$plugins_data = get_transient($this->transient_key); |
| 71 |
if (!empty($plugins_data)) return $plugins_data; |
| 72 |
|
| 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'; |
| 75 |
|
| 76 |
// Make the request to fetch the file contents |
| 77 |
$response = wp_remote_get($file_url); |
| 78 |
|
| 79 |
|
| 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); |
| 84 |
|
| 85 |
// Decode the JSON data into an array |
| 86 |
$data_array = json_decode($file_contents, true); |
| 87 |
|
| 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 = []; |
| 92 |
|
| 93 |
foreach ($data_array as $plugin_data) { |
| 94 |
|
| 95 |
$plugins_data[ $plugin_data['slug'] ] = $plugin_data; |
| 96 |
|
| 97 |
if ( str_contains( $plugin_data['download_link'], 'downloads.wordpress.org' ) ) { |
| 98 |
|
| 99 |
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
| 100 |
|
| 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 |
|
| 116 |
} |
| 117 |
|
| 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'); |
| 125 |
} |
| 126 |
} else { |
| 127 |
// Request failed |
| 128 |
echo esc_html__('Failed to retrieve file from Source.', 'adminify'); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Plugins List |
| 135 |
* |
| 136 |
* @author Jewel Theme <support@jeweltheme.com> |
| 137 |
*/ |
| 138 |
public function plugins_list() |
| 139 |
{ |
| 140 |
return $this->get_adminify_plugins_lists(); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Admin submenu |
| 145 |
*/ |
| 146 |
public function admin_menu() |
| 147 |
{ |
| 148 |
$submenu_position = apply_filters('jltwp_adminify_submenu_position', 50); |
| 149 |
add_submenu_page( |
| 150 |
'wp-adminify-settings', // Ex. wp-adminify-settings / edit.php?post_type=page . |
| 151 |
__('Addons', 'adminify'), |
| 152 |
sprintf( '<span class="adminify-addons-text">%s</span>', __( 'Addons', 'adminify' ) ), |
| 153 |
'manage_options', |
| 154 |
'wp-adminify-addons-plugins', |
| 155 |
array($this, 'render_addons_plugins'), |
| 156 |
$submenu_position |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|