| 1 |
<?php |
| 2 |
/** |
| 3 |
* QuadLayers WP Plugin Suggestions |
| 4 |
* |
| 5 |
* @package quadlayers/wp-plugin-suggestions |
| 6 |
* @author QuadLayers |
| 7 |
* @link https://github.com/quadlayers/wp-plugin-suggestions |
| 8 |
* @copyright Copyright (c) 2023 |
| 9 |
* @license GPL-3.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace QuadLayers\WP_Plugin_Suggestions; |
| 13 |
|
| 14 |
require_once ABSPATH . 'wp-admin/includes/class-wp-plugin-install-list-table.php'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Table |
| 18 |
*/ |
| 19 |
class Table extends \WP_Plugin_Install_List_Table { |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugins data. |
| 23 |
* |
| 24 |
* @var array Plugin data. |
| 25 |
*/ |
| 26 |
private $plugin_data = array( |
| 27 |
'author' => 'quadlayers', |
| 28 |
'per_page' => 36, |
| 29 |
'exclude' => array(), |
| 30 |
); |
| 31 |
|
| 32 |
/** |
| 33 |
* Table constructor. |
| 34 |
* |
| 35 |
* @param array $plugin_data Plugins data. |
| 36 |
*/ |
| 37 |
public function __construct( array $plugin_data = array() ) { |
| 38 |
/** |
| 39 |
* Merge plugin data with default data |
| 40 |
*/ |
| 41 |
$this->plugin_data = wp_parse_args( |
| 42 |
$plugin_data, |
| 43 |
$this->plugin_data |
| 44 |
); |
| 45 |
$this->maybe_patch_admin_urls(); |
| 46 |
|
| 47 |
parent::__construct(); |
| 48 |
} |
| 49 |
|
| 50 |
private function maybe_patch_admin_urls() { |
| 51 |
if ( is_multisite() && is_network_admin() ) { |
| 52 |
add_filter( 'self_admin_url', [ $this, 'fix_self_admin_url' ], 10, 2 ); |
| 53 |
add_filter( 'network_admin_url', [ $this, 'fix_network_admin_url' ], 10, 2 ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the transient key. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
private function get_transient_key() { |
| 63 |
|
| 64 |
$key = md5( serialize( $this->plugin_data ) ); |
| 65 |
|
| 66 |
return 'quadlayers_plugin_suggestions_' . $key; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Remove excluded plugins. |
| 71 |
* |
| 72 |
* @param array $plugins Plugins. |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
private function remove_excluded_plugins( $plugins ) { |
| 77 |
|
| 78 |
if ( empty( $this->plugin_data['exclude'] ) ) { |
| 79 |
return $plugins; |
| 80 |
} |
| 81 |
|
| 82 |
foreach ( $plugins as $key => $plugin ) { |
| 83 |
if ( in_array( $plugin['slug'], $this->plugin_data['exclude'] ) ) { |
| 84 |
unset( $plugins[ $key ] ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $plugins; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get plugins. |
| 93 |
* |
| 94 |
* @return array|mixed|object |
| 95 |
*/ |
| 96 |
public function get_plugins() { |
| 97 |
|
| 98 |
$tk = $this->get_transient_key(); |
| 99 |
|
| 100 |
$plugins = get_transient( $tk ); |
| 101 |
|
| 102 |
if ( $plugins === false ) { |
| 103 |
|
| 104 |
$args = array( |
| 105 |
'per_page' => $this->plugin_data['per_page'], |
| 106 |
'author' => $this->plugin_data['author'], |
| 107 |
'locale' => get_user_locale(), |
| 108 |
); |
| 109 |
|
| 110 |
$api = plugins_api( 'query_plugins', $args ); |
| 111 |
|
| 112 |
if ( ! is_wp_error( $api ) ) { |
| 113 |
|
| 114 |
$plugins = $this->remove_excluded_plugins( $api->plugins ); |
| 115 |
|
| 116 |
set_transient( $tk, $plugins, 24 * HOUR_IN_SECONDS ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $plugins; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Prepare items. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function prepare_items() { |
| 129 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 130 |
|
| 131 |
global $tabs, $tab; |
| 132 |
|
| 133 |
wp_enqueue_style( 'thickbox' ); |
| 134 |
wp_enqueue_script( 'plugin-install' ); |
| 135 |
wp_enqueue_script( 'thickbox' ); |
| 136 |
wp_enqueue_script( 'updates' ); |
| 137 |
wp_reset_vars( array( 'tab' ) ); |
| 138 |
|
| 139 |
$tabs = array(); |
| 140 |
|
| 141 |
if ( 'search' === $tab ) { |
| 142 |
$tabs['search'] = esc_html__( 'Search Results' ); |
| 143 |
} |
| 144 |
if ( $tab === 'beta' || false !== strpos( get_bloginfo( 'version' ), '-' ) ) { |
| 145 |
$tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' ); |
| 146 |
} |
| 147 |
$tabs['featured'] = _x( 'Featured', 'Plugin Installer' ); |
| 148 |
$tabs['popular'] = _x( 'Popular', 'Plugin Installer' ); |
| 149 |
$tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' ); |
| 150 |
$tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' ); |
| 151 |
|
| 152 |
$nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item. |
| 153 |
|
| 154 |
$tabs = apply_filters( 'install_plugins_tabs', $tabs ); |
| 155 |
|
| 156 |
$nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs ); |
| 157 |
|
| 158 |
// If a non-valid menu tab has been selected, And it's not a non-menu action. |
| 159 |
if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) ) { |
| 160 |
$tab = key( $tabs ); |
| 161 |
} |
| 162 |
|
| 163 |
$this->items = $this->get_plugins(); |
| 164 |
|
| 165 |
wp_localize_script( |
| 166 |
'updates', |
| 167 |
'_wpUpdatesItemCounts', |
| 168 |
array( |
| 169 |
'totals' => wp_get_update_data(), |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
} |