PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.2.8
Ever Compare – Products Compare Plugin for WooCommerce v1.2.8
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / includes / classes / Admin / Recommended_Plugins.php

Recommended_Plugins.php in Ever Compare – Products Compare Plugin for WooCommerce 1.2.8, at includes/classes/Admin/Recommended_Plugins.php

427 lines 19.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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', 'ever-compare' );
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['nonce'] = wp_create_nonce('htrp_nonce');
126 $localize_vars['buttontxt'] = array(
127 'buynow' => esc_html__( 'Buy Now', 'ever-compare' ),
128 'preview' => esc_html__( 'Preview', 'ever-compare' ),
129 'installing' => esc_html__( 'Installing..', 'ever-compare' ),
130 'activating' => esc_html__( 'Activating..', 'ever-compare' ),
131 'active' => esc_html__( 'Activated', 'ever-compare' ),
132 );
133 wp_localize_script( 'htrp-plugin-install-manager', 'htrp_params', $localize_vars );
134
135 }
136
137 /**
138 * [add_new_tab]
139 * @param [void] set tab content
140 */
141 public function add_new_tab( $tab_list ){
142 $this->tab_list[] = $tab_list;
143 }
144
145 /**
146 * [render_html]
147 * @return [void] Render HTML
148 */
149 public function render_html(){
150 if ( ! function_exists('plugins_api') ){ include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); }
151
152 $htplugins_plugin_list = $this->get_plugins();
153 $palscode_plugin_list = $this->get_plugins( 'palscode' );
154
155 $plugin_list = array_merge( $htplugins_plugin_list, $palscode_plugin_list );
156
157 $prepare_plugin = array();
158 foreach ( $plugin_list as $plugin_key => $plugin ) {
159 $prepare_plugin[$plugin['slug']] = $plugin;
160 }
161
162 ?>
163 <div class="wrap">
164 <h2><?php echo esc_html(get_admin_page_title()); ?></h2>
165 <style>
166 .htrp-admin-tab-pane{
167 display: none;
168 }
169 .htrp-admin-tab-pane.htrp-active{
170 display: block;
171 }
172 .htrp-extension-admin-tab-area .filter-links li>a:focus, .htrp-extension-admin-tab-area .filter-links li>a:hover {
173 color: inherit;
174 box-shadow: none;
175 }
176 .filter-links .htrp-active{
177 box-shadow: none;
178 border-bottom: 4px solid #646970;
179 color: #1d2327;
180 }
181 .downloaded-count{
182 display: block;
183 margin-top:5px;
184 }
185 </style>
186
187 <div class="htrp-extension-admin-tab-area wp-filter">
188 <ul class="htrp-admin-tabs filter-links">
189 <?php
190 foreach( $this->tab_list as $tab ){
191 $active_class = isset( $tab['active'] ) && $tab['active'] ? 'htrp-active' : '';
192 ?>
193 <li>
194 <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>
195 </li>
196 <?php
197 }
198 ?>
199 </ul>
200 </div>
201
202 <?php
203 $plugins_type = '';
204 foreach( $this->tab_list as $tab ):
205
206 $active_class = isset( $tab['active'] ) && $tab['active'] ? 'htrp-active' : '';
207 $plugins = $tab['plugins'];
208
209 echo '<div id="'.esc_attr( sanitize_title_with_dashes( $tab['title'] ) ).'" class="htrp-admin-tab-pane '.esc_attr( $active_class ).'">';
210 foreach( $plugins as $plugin ):
211
212 $data = array(
213 'slug' => isset( $plugin['slug'] ) ? $plugin['slug'] : '',
214 'location' => isset( $plugin['location'] ) ? $plugin['slug'].'/'.$plugin['location'] : '',
215 'name' => isset( $plugin['name'] ) ? $plugin['name'] : '',
216 );
217 $title = wp_kses( $plugin['name'], $this->plugins_allowedtags );
218
219 if( array_key_exists( $plugin['slug'], $prepare_plugin ) ){
220 $plugins_type = 'free';
221 $image_url = $this->plugin_icon( $plugins_type, $prepare_plugin[$data['slug']]['icons'] );
222 $description = wp_strip_all_tags( $prepare_plugin[$data['slug']]['description'] );
223 $author_name = wp_kses( $prepare_plugin[$data['slug']]['author'], $this->plugins_allowedtags );
224 $details_link = self_admin_url('plugin-install.php?tab=plugin-information&amp;plugin=' . $plugin['slug'] .'&amp;TB_iframe=true&amp;width=772&amp;height=577');
225 $target = '_self';
226 $modal_class = 'thickbox open-plugin-details-modal';
227
228 }else{
229 $plugins_type = 'pro';
230 $image_url = $this->plugin_icon( $plugins_type, $plugin['slug'] );
231 $description = isset( $plugin['description'] ) ? $plugin['description'] : '';
232 $author_name = esc_html__( 'HasTheme', 'ever-compare' );
233 $author_link = isset( $plugin['author_link'] ) ? $plugin['author_link'] : '';
234 $details_link = isset( $plugin['link'] ) ? $plugin['link'] : '';
235 $button_text = esc_html__('Buy Now', 'ever-compare' );
236 $button_classes = 'button button-primary';
237 $target = '_blank';
238 $modal_class = '';
239 }
240
241 if ( ! is_wp_error( $data ) ):
242
243 // Installed but Inactive.
244 if ( file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) && is_plugin_inactive( $data['location'] ) ) {
245
246 $button_classes = 'button htrp-activate-now button-primary';
247 $button_text = esc_html__( 'Activate', 'ever-compare' );
248
249 // Not Installed.
250 } elseif ( ! file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) ) {
251
252 $button_classes = 'button htrp-install-now';
253 $button_text = esc_html__( 'Install Now', 'ever-compare' );
254
255 // Active.
256 } else {
257 $button_classes = 'button disabled';
258 $button_text = esc_html__( 'Activated', 'ever-compare' );
259 }
260
261 ?>
262 <div class="plugin-card htrp-plugin-<?php echo sanitize_html_class( $plugin['slug'] ); ?>">
263 <div class="plugin-card-top">
264 <div class="name column-name" style="margin-right: 0;">
265 <h3>
266 <a href="<?php echo esc_url( $details_link ) ?>" target="<?php echo esc_attr( $target ) ?>" class="<?php echo esc_attr($modal_class); ?>" >
267 <?php echo esc_html( $title ) ?>
268 <img src="<?php echo esc_url( $image_url ) ?>" class="plugin-icon" alt="<?php echo esc_attr( $title ) ?>">
269 </a>
270 </h3>
271 </div>
272 <div class="desc column-description" style="margin-right: 0;">
273 <p><?php echo esc_html(wp_trim_words( $description, 23, '....')); ?></p>
274 <p class="authors">
275 <cite><?php echo esc_html__( 'By ', 'ever-compare' ); ?>
276 <?php if( $plugins_type == 'free' ): ?>
277 <?php echo wp_kses_post( $author_name); ?>
278 <?php else: ?>
279 <a href="<?php echo esc_url( $author_link ); ?>" target="_blank" ><?php echo esc_html($author_name); ?></a>
280 <?php endif; ?>
281 </cite>
282 </p>
283 </div>
284 </div>
285 <div class="plugin-card-bottom">
286 <div class="column-updated">
287 <?php
288 if (! file_exists( WP_PLUGIN_DIR . '/' . $data['location'] ) && $plugins_type == 'pro' ) {
289 echo '<a class="button button-primary" href="'.esc_url( $details_link ).'" target="'.esc_attr( $target ).'">'.esc_html__( 'Buy Now', 'ever-compare' ).'</a>';
290 }else{
291 ?>
292 <button class="<?php echo esc_attr($button_classes); ?>" data-pluginopt='<?php echo wp_json_encode( $data ); ?>'><?php echo esc_html($button_text); ?></button>
293
294 <?php } ?>
295 </div>
296 <div class="column-downloaded">
297 <a href="<?php echo esc_url( $details_link ) ?>" target="<?php echo esc_attr( $target ) ?>" class=" <?php echo esc_attr($modal_class); ?>" ><?php echo esc_html__('More Details', 'ever-compare') ?></a>
298 <span class="downloaded-count">
299 <?php
300 if( $plugins_type == 'free' ){
301 /* translators: %s: Number of installations. */
302 printf( esc_html__( '%s Active Installations' ), esc_html($this->active_install_count( $prepare_plugin[$data['slug']]['active_installs'] )) );
303 }
304 ?>
305 </span>
306 </div>
307 </div>
308 </div>
309 <?php
310 endif;
311 endforeach;
312 echo '</div>';
313
314 endforeach;
315 ?>
316
317 </div>
318 <?php
319
320 }
321
322 /**
323 * [get_plugins] Get plugin from wp.org API
324 * @param string $username wo.org username
325 * @return [array] plugin list
326 */
327 public function get_plugins( $username = 'htplugins' ){
328 $transient_var = 'htrp_htplugins_list_'.$username;
329 $org_plugins_list = get_transient( $transient_var );
330
331 if ( false === $org_plugins_list ) {
332 $plugins_list_by_author = plugins_api( 'query_plugins', array( 'author' => $username, 'per_page' => 100 ) );
333 set_transient( $transient_var, $plugins_list_by_author->plugins, 1 * WEEK_IN_SECONDS );
334 $org_plugins_list = $plugins_list_by_author->plugins;
335 }
336
337 return $org_plugins_list;
338 }
339
340 /**
341 * [plugin_icon] Generate plugin icon
342 * @param string $type plugin type
343 * @param [array|string] $icon
344 * @return [URL] icon URL
345 */
346 public function plugin_icon( $type = 'free', $icon = '' ){
347 if( $type === 'free' ){
348 if ( ! empty( $icon['svg'] ) ) {
349 $plugin_icon_url = $icon['svg'];
350 } elseif ( ! empty( $icon['2x'] ) ) {
351 $plugin_icon_url = $icon['2x'];
352 } elseif ( ! empty( $icon['1x'] ) ) {
353 $plugin_icon_url = $icon['1x'];
354 } else {
355 $plugin_icon_url = $icon['default'];
356 }
357 }else{
358 $plugin_icon_url = $this->assets_url .'/images/extensions/'.$icon.'.png';
359 }
360
361 return $plugin_icon_url;
362
363 }
364
365 /**
366 * [active_install_count] Manage Active install count
367 * @param [int] $active_installs
368 * @return [string]
369 */
370 public function active_install_count( $active_installs ){
371
372 if ( $active_installs >= 1000000 ) {
373 $active_installs_millions = floor( $active_installs / 1000000 );
374 $active_installs_text = sprintf(
375 /* translators: %s: Number of millions. */
376 _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
377 number_format_i18n( $active_installs_millions )
378 );
379 } elseif ( 0 === $active_installs ) {
380 $active_installs_text = _x( 'Less Than 10', 'Active plugin installations' );
381 } else {
382 $active_installs_text = number_format_i18n( $active_installs ) . '+';
383 }
384 return $active_installs_text;
385
386 }
387
388 /**
389 * [plugin_activation] Plugin activation ajax callable function
390 * @return [JSON]
391 */
392 public function plugin_activation() {
393
394 check_ajax_referer('htrp_nonce', 'nonce');
395
396 if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['location'] ) || ! $_POST['location'] ) {
397 wp_send_json_error(
398 array(
399 'success' => false,
400 'message' => esc_html__( 'Plugin Not Found', 'ever-compare' ),
401 )
402 );
403 }
404
405 $plugin_location = ( isset( $_POST['location'] ) ) ? sanitize_text_field( $_POST['location'] ) : '';
406 $activate = activate_plugin( $plugin_location, '', false, true );
407
408 if ( is_wp_error( $activate ) ) {
409 wp_send_json_error(
410 array(
411 'success' => false,
412 'message' => $activate->get_error_message(),
413 )
414 );
415 }
416
417 wp_send_json_success(
418 array(
419 'success' => true,
420 'message' => esc_html__( 'Plugin Successfully Activated', 'ever-compare' ),
421 )
422 );
423
424 }
425
426
427 }