| 1 |
<?php |
| 2 |
namespace EverCompare\Admin; |
| 3 |
/** |
| 4 |
* Recommended Plugins handlers class |
| 5 |
*/ |
| 6 |
class Recommended_Plugins { |
| 7 |
|
| 8 |
/** |
| 9 |
* [$_instance] |
| 10 |
* @var null |
| 11 |
*/ |
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* [$plugins_allowedtags] allow tag |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
public $plugins_allowedtags = array( |
| 19 |
'a' => array( |
| 20 |
'href' => array(), |
| 21 |
'title' => array(), |
| 22 |
'target' => array(), |
| 23 |
), |
| 24 |
'abbr' => array( 'title' => array() ), |
| 25 |
'acronym' => array( 'title' => array() ), |
| 26 |
'code' => array(), |
| 27 |
'pre' => array(), |
| 28 |
'em' => array(), |
| 29 |
'strong' => array(), |
| 30 |
'ul' => array(), |
| 31 |
'ol' => array(), |
| 32 |
'li' => array(), |
| 33 |
'p' => array(), |
| 34 |
'br' => array(), |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Veriable Initialize |
| 39 |
*/ |
| 40 |
public $text_domain = ''; |
| 41 |
public $parent_menu_slug = ''; |
| 42 |
public $menu_label = ''; |
| 43 |
public $menu_page_slug = ''; |
| 44 |
public $menu_capability = ''; |
| 45 |
public $priority = ''; |
| 46 |
public $hook_suffix = ''; |
| 47 |
public $assets_url = ''; |
| 48 |
public $tab_list = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* [instance] Initializes a singleton instance |
| 52 |
* @return [Recommended_Plugins] |
| 53 |
*/ |
| 54 |
public static function instance( $args = [] ) { |
| 55 |
if ( is_null( self::$_instance ) ) { |
| 56 |
self::$_instance = new self( $args ); |
| 57 |
} |
| 58 |
return self::$_instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* [__construct] Class construct |
| 63 |
*/ |
| 64 |
function __construct( $args ) { |
| 65 |
|
| 66 |
// Initialize properties |
| 67 |
$this->text_domain = !empty( $args['text_domain'] ) ? $args['text_domain'] : 'htrp'; |
| 68 |
$this->parent_menu_slug = !empty( $args['parent_menu_slug'] ) ? $args['parent_menu_slug'] : 'plugins.php'; |
| 69 |
$this->menu_label = !empty( $args['menu_label'] ) ? $args['menu_label'] : esc_html__( 'Recommendations', $this->text_domain ); |
| 70 |
$this->menu_capability = !empty( $args['menu_capability'] ) ? $args['menu_capability'] : 'manage_options'; |
| 71 |
$this->menu_page_slug = !empty( $args['menu_page_slug'] ) ? $args['menu_page_slug'] : $this->text_domain . '_extensions'; |
| 72 |
$this->priority = !empty( $args['priority'] ) ? $args['priority'] : 100; |
| 73 |
$this->hook_suffix = !empty( $args['hook_suffix'] ) ? $args['hook_suffix'] : ''; |
| 74 |
$this->assets_url = !empty( $args['assets_url'] ) ? $args['assets_url'] : plugins_url( '', __FILE__ ); |
| 75 |
$this->tab_list = !empty( $args['tab_list'] ) ? $args['assets_url'] : []; |
| 76 |
|
| 77 |
|
| 78 |
add_action( 'admin_menu', [ $this, 'admin_menu' ], $this->priority ); |
| 79 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 80 |
|
| 81 |
// Ajax Action |
| 82 |
add_action( 'wp_ajax_'.$this->text_domain.'_ajax_plugin_activation', [ $this, 'plugin_activation' ] ); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* [admin_menu] Add Recommended Menu |
| 88 |
* @return [void] |
| 89 |
*/ |
| 90 |
public function admin_menu(){ |
| 91 |
add_submenu_page( |
| 92 |
$this->parent_menu_slug, |
| 93 |
$this->menu_label, |
| 94 |
$this->menu_label, |
| 95 |
$this->menu_capability, |
| 96 |
$this->menu_page_slug, |
| 97 |
[ $this, 'render_html' ] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* [enqueue_assets] |
| 103 |
* @param [string] $hook_suffix Current page hook |
| 104 |
* @return [void] |
| 105 |
*/ |
| 106 |
public function enqueue_assets( $hook_suffix ) { |
| 107 |
if( $this->hook_suffix ){ |
| 108 |
if( $this->hook_suffix == $hook_suffix ){ |
| 109 |
wp_enqueue_script( 'htrp-plugin-install-manager', $this->assets_url . '/js/plugins_install_manager.js', array('jquery','wp-util', 'updates'), '1.0.0', true ); |
| 110 |
} |
| 111 |
} else { |
| 112 |
wp_enqueue_script( 'htrp-plugin-install-manager', $this->assets_url . '/js/plugins_install_manager.js', array('jquery','wp-util', 'updates'), '1.0.0', true ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Thickbox assest |
| 117 |
*/ |
| 118 |
add_thickbox(); |
| 119 |
|
| 120 |
/** |
| 121 |
* localize data |
| 122 |
*/ |
| 123 |
$localize_vars['ajaxurl'] = admin_url('admin-ajax.php'); |
| 124 |
$localize_vars['text_domain'] = sanitize_title_with_dashes( $this->text_domain ); |
| 125 |
$localize_vars['buttontxt'] = array( |
| 126 |
'buynow' => esc_html__( 'Buy Now', $this->text_domain ), |
| 127 |
'preview' => esc_html__( 'Preview', $this->text_domain ), |
| 128 |
'installing' => esc_html__( 'Installing..', $this->text_domain ), |
| 129 |
'activating' => esc_html__( 'Activating..', $this->text_domain ), |
| 130 |
'active' => esc_html__( 'Activated', $this->text_domain ), |
| 131 |
); |
| 132 |
wp_localize_script( 'htrp-plugin-install-manager', 'htrp_params', $localize_vars ); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* [add_new_tab] |
| 138 |
* @param [void] set tab content |
| 139 |
*/ |
| 140 |
public function add_new_tab( $tab_list ){ |
| 141 |
$this->tab_list[] = $tab_list; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* [render_html] |
| 146 |
* @return [void] Render HTML |
| 147 |
*/ |
| 148 |
public function render_html(){ |
| 149 |
if ( ! function_exists('plugins_api') ){ include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); } |
| 150 |
|
| 151 |
$htplugins_plugin_list = $this->get_plugins(); |
| 152 |
$palscode_plugin_list = $this->get_plugins( 'palscode' ); |
| 153 |
|
| 154 |
$plugin_list = array_merge( $htplugins_plugin_list, $palscode_plugin_list ); |
| 155 |
|
| 156 |
$prepare_plugin = array(); |
| 157 |
foreach ( $plugin_list as $plugin_key => $plugin ) { |
| 158 |
$prepare_plugin[$plugin['slug']] = $plugin; |
| 159 |
} |
| 160 |
|
| 161 |
?> |
| 162 |
<div class="wrap"> |
| 163 |
<h2><?php echo get_admin_page_title(); ?></h2> |
| 164 |
<style> |
| 165 |
.htrp-admin-tab-pane{ |
| 166 |
display: none; |
| 167 |
} |
| 168 |
.htrp-admin-tab-pane.htrp-active{ |
| 169 |
display: block; |
| 170 |
} |
| 171 |
.htrp-extension-admin-tab-area .filter-links li>a:focus, .htrp-extension-admin-tab-area .filter-links li>a:hover { |
| 172 |
color: inherit; |
| 173 |
box-shadow: none; |
| 174 |
} |
| 175 |
.filter-links .htrp-active{ |
| 176 |
box-shadow: none; |
| 177 |
border-bottom: 4px solid #646970; |
| 178 |
color: #1d2327; |
| 179 |
} |
| 180 |
.downloaded-count{ |
| 181 |
display: block; |
| 182 |
margin-top:5px; |
| 183 |
} |
| 184 |
</style> |
| 185 |
|
| 186 |
<div class="htrp-extension-admin-tab-area wp-filter"> |
| 187 |
<ul class="htrp-admin-tabs filter-links"> |
| 188 |
<?php |
| 189 |
foreach( $this->tab_list as $tab ){ |
| 190 |
$active_class = isset( $tab['active'] ) && $tab['active'] ? 'htrp-active' : ''; |
| 191 |
?> |
| 192 |
<li> |
| 193 |
<a href="#<?php echo esc_attr( sanitize_title_with_dashes( $tab['title'] ) ) ?>" class="<?php echo esc_attr( $active_class ) ?>"><?php echo esc_html( $tab['title'] ) ?></a> |
| 194 |
</li> |
| 195 |
<?php |
| 196 |
} |
| 197 |
?> |
| 198 |
</ul> |
| 199 |
</div> |
| 200 |
|
| 201 |
<?php |
| 202 |
$plugins_type = ''; |
| 203 |
foreach( $this->tab_list as $tab ): |
| 204 |
|
| 205 |
$active_class = isset( $tab['active'] ) && $tab['active'] ? 'htrp-active' : ''; |
| 206 |
$plugins = $tab['plugins']; |
| 207 |
|
| 208 |
echo '<div id="'.esc_attr( sanitize_title_with_dashes( $tab['title'] ) ).'" class="htrp-admin-tab-pane '.esc_attr( $active_class ).'">'; |
| 209 |
foreach( $plugins as $plugin ): |
| 210 |
|
| 211 |
$data = array( |
| 212 |
'slug' => isset( $plugin['slug'] ) ? $plugin['slug'] : '', |
| 213 |
'location' => isset( $plugin['location'] ) ? $plugin['slug'].'/'.$plugin['location'] : '', |
| 214 |
'name' => isset( $plugin['name'] ) ? $plugin['name'] : '', |
| 215 |
); |
| 216 |
$title = wp_kses( $plugin['name'], $this->plugins_allowedtags ); |
| 217 |
|
| 218 |
if( array_key_exists( $plugin['slug'], $prepare_plugin ) ){ |
| 219 |
$plugins_type = 'free'; |
| 220 |
$image_url = $this->plugin_icon( $plugins_type, $prepare_plugin[$data['slug']]['icons'] ); |
| 221 |
$description = strip_tags( $prepare_plugin[$data['slug']]['description'] ); |
| 222 |
$author_name = wp_kses( $prepare_plugin[$data['slug']]['author'], $this->plugins_allowedtags ); |
| 223 |
$details_link = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .'&TB_iframe=true&width=772&height=577'); |
| 224 |
$target = '_self'; |
| 225 |
$modal_class = 'class="thickbox open-plugin-details-modal"'; |
| 226 |
|
| 227 |
}else{ |
| 228 |
$plugins_type = 'pro'; |
| 229 |
$image_url = $this->plugin_icon( $plugins_type, $plugin['slug'] ); |
| 230 |
$description = isset( $plugin['description'] ) ? $plugin['description'] : ''; |
| 231 |
$author_name = esc_html__( 'HasTheme', $this->text_domain ); |
| 232 |
$author_link = isset( $plugin['author_link'] ) ? $plugin['author_link'] : ''; |
| 233 |
$details_link = isset( $plugin['link'] ) ? $plugin['link'] : ''; |
| 234 |
$button_text = esc_html__('Buy Now', $this->text_domain ); |
| 235 |
$button_classes = 'button button-primary'; |
| 236 |
$target = '_blank'; |
| 237 |
$modal_class = ''; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! is_wp_error( $data ) ): |
| 241 |
|
| 242 |
// Installed but Inactive. |
| 243 |
if ( file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) && is_plugin_inactive( $data['location'] ) ) { |
| 244 |
|
| 245 |
$button_classes = 'button htrp-activate-now button-primary'; |
| 246 |
$button_text = esc_html__( 'Activate', $this->text_domain ); |
| 247 |
|
| 248 |
// Not Installed. |
| 249 |
} elseif ( ! file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) ) { |
| 250 |
|
| 251 |
$button_classes = 'button htrp-install-now'; |
| 252 |
$button_text = esc_html__( 'Install Now', $this->text_domain ); |
| 253 |
|
| 254 |
// Active. |
| 255 |
} else { |
| 256 |
$button_classes = 'button disabled'; |
| 257 |
$button_text = esc_html__( 'Activated', $this->text_domain ); |
| 258 |
} |
| 259 |
|
| 260 |
?> |
| 261 |
<div class="plugin-card htrp-plugin-<?php echo sanitize_html_class( $plugin['slug'] ); ?>"> |
| 262 |
<div class="plugin-card-top"> |
| 263 |
<div class="name column-name" style="margin-right: 0;"> |
| 264 |
<h3> |
| 265 |
<a href="<?php echo esc_url( $details_link ) ?>" target="<?php echo esc_attr( $target ) ?>" <?php echo $modal_class; ?>> |
| 266 |
<?php echo esc_html( $title ) ?> |
| 267 |
<img src="<?php echo esc_url( $image_url ) ?>" class="plugin-icon" alt="<?php echo esc_attr( $title ) ?>"> |
| 268 |
</a> |
| 269 |
</h3> |
| 270 |
</div> |
| 271 |
<div class="desc column-description" style="margin-right: 0;"> |
| 272 |
<p><?php echo wp_trim_words( $description, 23, '....'); ?></p> |
| 273 |
<p class="authors"> |
| 274 |
<cite><?php echo esc_html__( 'By ', $this->text_domain ); ?> |
| 275 |
<?php if( $plugins_type == 'free' ): ?> |
| 276 |
<?php echo $author_name; ?> |
| 277 |
<?php else: ?> |
| 278 |
<a href="<?php echo esc_url( $author_link ); ?>" target="_blank" ><?php echo $author_name; ?></a> |
| 279 |
<?php endif; ?> |
| 280 |
</cite> |
| 281 |
</p> |
| 282 |
</div> |
| 283 |
</div> |
| 284 |
<div class="plugin-card-bottom"> |
| 285 |
<div class="column-updated"> |
| 286 |
<?php |
| 287 |
if (! file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) && $plugins_type == 'pro' ) { |
| 288 |
echo '<a class="button button-primary" href="'.esc_url( $details_link ).'" target="'.esc_attr( $target ).'">'.esc_html__( 'Buy Now', $this->text_domain ).'</a>'; |
| 289 |
}else{ |
| 290 |
?> |
| 291 |
<button class="<?php echo $button_classes; ?>" data-pluginopt='<?php echo wp_json_encode( $data ); ?>'><?php echo $button_text; ?></button> |
| 292 |
|
| 293 |
<?php } ?> |
| 294 |
</div> |
| 295 |
<div class="column-downloaded"> |
| 296 |
<a href="<?php echo esc_url( $details_link ) ?>" target="<?php echo esc_attr( $target ) ?>" <?php echo $modal_class; ?>><?php echo esc_html__('More Details', $this->text_domain) ?></a> |
| 297 |
<span class="downloaded-count"> |
| 298 |
<?php |
| 299 |
if( $plugins_type == 'free' ){ |
| 300 |
/* translators: %s: Number of installations. */ |
| 301 |
printf( __( '%s Active Installations' ), $this->active_install_count( $prepare_plugin[$data['slug']]['active_installs'] ) ); |
| 302 |
} |
| 303 |
?> |
| 304 |
</span> |
| 305 |
</div> |
| 306 |
</div> |
| 307 |
</div> |
| 308 |
<?php |
| 309 |
endif; |
| 310 |
endforeach; |
| 311 |
echo '</div>'; |
| 312 |
|
| 313 |
endforeach; |
| 314 |
?> |
| 315 |
|
| 316 |
</div> |
| 317 |
<?php |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* [get_plugins] Get plugin from wp.org API |
| 323 |
* @param string $username wo.org username |
| 324 |
* @return [array] plugin list |
| 325 |
*/ |
| 326 |
public function get_plugins( $username = 'htplugins' ){ |
| 327 |
$transient_var = 'htrp_htplugins_list_'.$username; |
| 328 |
$org_plugins_list = get_transient( $transient_var ); |
| 329 |
|
| 330 |
if ( false === $org_plugins_list ) { |
| 331 |
$plugins_list_by_author = plugins_api( 'query_plugins', array( 'author' => $username, 'per_page' => 100 ) ); |
| 332 |
set_transient( $transient_var, $plugins_list_by_author->plugins, 1 * WEEK_IN_SECONDS ); |
| 333 |
$org_plugins_list = $plugins_list_by_author->plugins; |
| 334 |
} |
| 335 |
|
| 336 |
return $org_plugins_list; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* [plugin_icon] Generate plugin icon |
| 341 |
* @param string $type plugin type |
| 342 |
* @param [array|string] $icon |
| 343 |
* @return [URL] icon URL |
| 344 |
*/ |
| 345 |
public function plugin_icon( $type = 'free', $icon = '' ){ |
| 346 |
if( $type === 'free' ){ |
| 347 |
if ( ! empty( $icon['svg'] ) ) { |
| 348 |
$plugin_icon_url = $icon['svg']; |
| 349 |
} elseif ( ! empty( $icon['2x'] ) ) { |
| 350 |
$plugin_icon_url = $icon['2x']; |
| 351 |
} elseif ( ! empty( $icon['1x'] ) ) { |
| 352 |
$plugin_icon_url = $icon['1x']; |
| 353 |
} else { |
| 354 |
$plugin_icon_url = $icon['default']; |
| 355 |
} |
| 356 |
}else{ |
| 357 |
$plugin_icon_url = $this->assets_url .'/images/extensions/'.$icon.'.png'; |
| 358 |
} |
| 359 |
|
| 360 |
return $plugin_icon_url; |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* [active_install_count] Manage Active install count |
| 366 |
* @param [int] $active_installs |
| 367 |
* @return [string] |
| 368 |
*/ |
| 369 |
public function active_install_count( $active_installs ){ |
| 370 |
|
| 371 |
if ( $active_installs >= 1000000 ) { |
| 372 |
$active_installs_millions = floor( $active_installs / 1000000 ); |
| 373 |
$active_installs_text = sprintf( |
| 374 |
/* translators: %s: Number of millions. */ |
| 375 |
_nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ), |
| 376 |
number_format_i18n( $active_installs_millions ) |
| 377 |
); |
| 378 |
} elseif ( 0 === $active_installs ) { |
| 379 |
$active_installs_text = _x( 'Less Than 10', 'Active plugin installations' ); |
| 380 |
} else { |
| 381 |
$active_installs_text = number_format_i18n( $active_installs ) . '+'; |
| 382 |
} |
| 383 |
return $active_installs_text; |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* [plugin_activation] Plugin activation ajax callable function |
| 389 |
* @return [JSON] |
| 390 |
*/ |
| 391 |
public function plugin_activation() { |
| 392 |
|
| 393 |
if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['location'] ) || ! $_POST['location'] ) { |
| 394 |
wp_send_json_error( |
| 395 |
array( |
| 396 |
'success' => false, |
| 397 |
'message' => esc_html__( 'Plugin Not Found', $this->text_domain ), |
| 398 |
) |
| 399 |
); |
| 400 |
} |
| 401 |
|
| 402 |
$plugin_location = ( isset( $_POST['location'] ) ) ? esc_attr( $_POST['location'] ) : ''; |
| 403 |
$activate = activate_plugin( $plugin_location, '', false, true ); |
| 404 |
|
| 405 |
if ( is_wp_error( $activate ) ) { |
| 406 |
wp_send_json_error( |
| 407 |
array( |
| 408 |
'success' => false, |
| 409 |
'message' => $activate->get_error_message(), |
| 410 |
) |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
wp_send_json_success( |
| 415 |
array( |
| 416 |
'success' => true, |
| 417 |
'message' => esc_html__( 'Plugin Successfully Activated', $this->text_domain ), |
| 418 |
) |
| 419 |
); |
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
} |