| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
|
| 6 |
if( ! class_exists( 'WP_List_Table' ) ) { |
| 7 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 8 |
} |
| 9 |
|
| 10 |
class CL_Addons_List_Table extends WP_List_Table { |
| 11 |
|
| 12 |
public $header; |
| 13 |
|
| 14 |
public $section_header; |
| 15 |
public $section_header_free; |
| 16 |
public $section_versions; |
| 17 |
public $sections; |
| 18 |
|
| 19 |
public $images_folder; |
| 20 |
public $text_domain = 'profile-builder'; |
| 21 |
|
| 22 |
public $all_addons; |
| 23 |
public $all_plugins; |
| 24 |
|
| 25 |
public $current_version; |
| 26 |
|
| 27 |
public $tooltip_header; |
| 28 |
public $tooltip_content; |
| 29 |
public $tooltip_content_license_inactive; |
| 30 |
|
| 31 |
function __construct(){ |
| 32 |
|
| 33 |
//Set parent defaults |
| 34 |
parent::__construct(array( |
| 35 |
'singular' => 'add-on', //singular name of the listed records |
| 36 |
'plural' => 'add-ons', //plural name of the listed records |
| 37 |
'ajax' => false //does this table support ajax? |
| 38 |
)); |
| 39 |
|
| 40 |
//this is necessary because of WP_List_Table |
| 41 |
$this->prepare_items(); |
| 42 |
|
| 43 |
//enqueue scripts and styles |
| 44 |
add_action('admin_footer', array($this, 'cl_print_assets')); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Print js and css |
| 50 |
* @param $hook |
| 51 |
*/ |
| 52 |
function cl_print_assets(){ |
| 53 |
wp_enqueue_style('wp-pointer'); |
| 54 |
wp_enqueue_script('wp-pointer'); |
| 55 |
|
| 56 |
wp_localize_script( |
| 57 |
'wp-pointer', |
| 58 |
'cl_add_ons_pointer', |
| 59 |
array( |
| 60 |
'tooltip_header' => $this->tooltip_header, |
| 61 |
'tooltip_content' => $this->tooltip_content, |
| 62 |
'tooltip_content_license_inactive' => $this->tooltip_content_license_inactive, |
| 63 |
'license_status' => wppb_get_serial_number_status(), |
| 64 |
'paid_version' => defined( 'WPPB_PAID_PLUGIN_DIR' ) ? true : false |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
wp_enqueue_style('cl-add-ons-listing-css', plugin_dir_url(__FILE__) . '/assets/css/cl-add-ons-listing.css', false); |
| 69 |
wp_enqueue_script('cl-add-ons-listing-js', plugin_dir_url(__FILE__) . '/assets/js/cl-add-ons-listing.js', array('jquery')); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Define the columns here and their headers |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
function get_columns(){ |
| 78 |
$columns = array( |
| 79 |
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 80 |
'icon' => '', |
| 81 |
'add_on' => __('Add-On', $this->text_domain ), //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 82 |
'actions' => '', |
| 83 |
); |
| 84 |
return $columns; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* The checkbox column |
| 89 |
* @param object $item |
| 90 |
* @return string|void |
| 91 |
*/ |
| 92 |
function column_cb($item){ |
| 93 |
if( $item['type'] === 'add-on') { |
| 94 |
in_array( $this->current_version, $this->section_versions ) ? $disabled = '' : $disabled = 'disabled'; |
| 95 |
return '<input type="checkbox" name="cl_add_ons[]" ' . checked($this->is_add_on_active($item['slug']), true, false) . ' '. $disabled .' value="' . $item['slug'] . '" />'; |
| 96 |
}elseif( $item['type'] === 'plugin') { |
| 97 |
$all_wp_plugins = get_plugins(); |
| 98 |
array_key_exists( $item['slug'], $all_wp_plugins ) ? $disabled = '' : $disabled = 'disabled';//add disabled if the current version isn't eligible |
| 99 |
if( empty($disabled) ){ |
| 100 |
is_plugin_active_for_network( $item['slug'] ) ? $disabled = 'disabled' : $disabled = ''; |
| 101 |
} |
| 102 |
|
| 103 |
return '<input type="checkbox" name="cl_plugins[]" ' . checked(is_plugin_active($item['slug']), true, false) . ' '. $disabled .' value="' . $item['slug'] . '" />'; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* The icon column |
| 109 |
* @param $item |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
function column_icon($item){ |
| 113 |
return '<img src="'.$this->images_folder. $item['icon'] .'" width="64" height="64" alt="'. $item['name'] .'">'; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* The column where we display the addon name and description |
| 118 |
* @param $item |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
function column_add_on($item){ |
| 122 |
return '<strong class="cl-add-ons-name">'. $item['name'] . '</strong><br/>'. $item['description']; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* The actions column for the addons |
| 127 |
* @param $item |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
function column_actions($item){ |
| 131 |
|
| 132 |
$action = ''; |
| 133 |
//for plugins we can do something general |
| 134 |
if( $item['type'] === 'plugin' ) { |
| 135 |
|
| 136 |
$all_wp_plugins = get_plugins(); |
| 137 |
if( array_key_exists( $item['slug'], $all_wp_plugins ) ) { |
| 138 |
if( is_plugin_active_for_network( $item['slug'] ) ){ |
| 139 |
$action = '<a class="right button button-secondary" href="' . esc_url(network_admin_url( 'plugins.php' )) . '">' . __('Manage in Network', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 140 |
} |
| 141 |
else { |
| 142 |
if( isset( $_REQUEST['page'] ) ) { |
| 143 |
if (is_plugin_active($item['slug'])) { |
| 144 |
$action = '<a class="right button button-secondary" href="' . esc_url(wp_nonce_url(add_query_arg('cl_plugins', $item['slug'], admin_url('admin.php?page=' . sanitize_text_field( $_REQUEST['page'] ) . '&cl_add_ons_action=deactivate')), 'cl_add_ons_action')) . '">' . __('Deactivate', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 145 |
} else { |
| 146 |
$action = '<a class="right button button-primary" href="' . esc_url(wp_nonce_url(add_query_arg('cl_plugins', $item['slug'], admin_url('admin.php?page=' .sanitize_text_field( $_REQUEST['page'] ) . '&cl_add_ons_action=activate')), 'cl_add_ons_action')) . '">' . __('Activate', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
else{ |
| 152 |
$action = '<a target="_blank" class="right button button-secondary" href="'. $item['download_url'] .'">' . __('Download', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
} |
| 157 |
elseif ( $item['type'] === 'add-on' ){//this is more complicated as there are multiple cases, I think it should be done through filters in each plugin |
| 158 |
|
| 159 |
//get license status |
| 160 |
$license_status = wppb_get_serial_number_status(); |
| 161 |
|
| 162 |
//set some default values |
| 163 |
in_array( $this->current_version, $this->section_versions ) ? $disabled = '' : $disabled = 'disabled'; //add disabled if the current version isn't eligible |
| 164 |
|
| 165 |
$onclick = ''; |
| 166 |
|
| 167 |
//construct the action URL based on the add-on status |
| 168 |
$base_url = admin_url( 'admin.php?page='. sanitize_text_field( $_REQUEST['page'] ) ); |
| 169 |
|
| 170 |
if ( $this->is_add_on_active( $item['slug'] ) ) |
| 171 |
$base_url .= '&cl_add_ons_action=deactivate'; |
| 172 |
else |
| 173 |
$base_url .= '&cl_add_ons_action=activate'; |
| 174 |
|
| 175 |
$action_url = esc_url( wp_nonce_url( add_query_arg( 'cl_add_ons', $item['slug'], $base_url ), 'cl_add_ons_action' ) ); |
| 176 |
|
| 177 |
//if we don't have a valid license we can't activate the add-on and throw a confirmation dialog |
| 178 |
if ( $license_status !== 'valid' && ( !isset( $item['version'] ) || $item['version'] != 'free' ) ) { |
| 179 |
|
| 180 |
if( $this->is_add_on_active( $item['slug'] ) ) { |
| 181 |
$onclick = 'onclick="return confirm(\'' . esc_js( __( 'This add-on can not be activated back without a valid license. Are you sure you want to deactivate it?', 'profile-builder' ) ) . '\')"'; |
| 182 |
// $action_url = ''; |
| 183 |
} else { |
| 184 |
$disabled = 'disabled'; //if the license is not valid, disable the button |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
if ( $this->is_add_on_active( $item['slug'] ) ) { |
| 190 |
$action = '<a class="right button button-secondary" '.$disabled.' href="'. $action_url .'" '.$onclick.'>' . __('Deactivate', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 191 |
} else { |
| 192 |
$action = '<a class="right button button-primary" '.$disabled.' href="'. $action_url .'">' . __('Activate', $this->text_domain) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
$documentation = '<a target="_blank" class="right" href="'. $item['doc_url'] . '">' . __( 'Documentation', $this->text_domain ) . '</a>'; //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 198 |
|
| 199 |
return $action . $documentation; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
//don't generate a bulk actions dropdown by returning an empty array |
| 204 |
function get_bulk_actions() { |
| 205 |
return array( );//don't show bulk actions |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Function that initializez the object properties |
| 212 |
*/ |
| 213 |
function prepare_items() { |
| 214 |
$columns = $this->get_columns(); |
| 215 |
$this->_column_headers = array($columns, array(), array());//the two empty arrays are hidden and sortable |
| 216 |
$this->set_pagination_args( array( ) ); //we do not need pagination |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
/** Here start the customizations for multiple tables and our custom html **/ |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* Show our own search box, we don't use the default search of Table listing |
| 225 |
*/ |
| 226 |
function show_search_box(){ |
| 227 |
?> |
| 228 |
<p class="cl-add-ons-search-box"> |
| 229 |
<input type="text" id="cl-add-ons-search-input" name="s" value="" placeholder="<?php esc_html_e( 'Search for add-ons...', $this->text_domain ); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain ?>"> |
| 230 |
</p> |
| 231 |
<?php |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Show the submit button |
| 236 |
*/ |
| 237 |
function show_sumbit_button(){ |
| 238 |
?> |
| 239 |
<input type="submit" class="button-primary" value="<?php esc_html_e('Save Add-ons', $this->text_domain); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain?>"> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* This is the function that adds more sections (tables) to the listing |
| 245 |
*/ |
| 246 |
function add_section(){ |
| 247 |
ob_start(); |
| 248 |
?> |
| 249 |
<div class="cl-add-ons-section"> |
| 250 |
<?php if( !empty( $this->section_header ) ): ?> |
| 251 |
|
| 252 |
<h2><?php echo esc_html ( $this->section_header['title'] );?></h2> |
| 253 |
|
| 254 |
<?php if( !$this->current_version != 'Profile Builder Free' && !empty( $this->section_header_free ) && !in_array( $this->current_version, $this->section_versions ) ) : ?> |
| 255 |
<p class="description cozmoslabs-description-upsell"><?php echo wp_kses_post( $this->section_header_free['description'] ); ?></p> |
| 256 |
<?php elseif( !empty( $this->section_header['description'] ) ) : ?> |
| 257 |
<p class="description"><?php echo esc_html( $this->section_header['description'] ); ?></p> |
| 258 |
<?php endif; ?> |
| 259 |
|
| 260 |
<?php endif; ?> |
| 261 |
|
| 262 |
<?php |
| 263 |
foreach( $this->items as $item ) { |
| 264 |
if( $item['type'] === 'add-on' ) |
| 265 |
$this->all_addons[] = $item['slug']; |
| 266 |
elseif( $item['type'] === 'plugin' ) |
| 267 |
$this->all_plugins[] = $item['slug']; |
| 268 |
} |
| 269 |
$this->display(); /* this is the function from the table listing class */ |
| 270 |
?> |
| 271 |
</div> |
| 272 |
<?php |
| 273 |
|
| 274 |
$output = ob_get_contents(); |
| 275 |
|
| 276 |
ob_end_clean(); |
| 277 |
|
| 278 |
$this->sections[] = $output; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* The function that actually displays all the tables and the surrounding html |
| 284 |
*/ |
| 285 |
function display_addons(){ |
| 286 |
?> |
| 287 |
<div class="wrap" id="cl-add-ons-listing"> |
| 288 |
|
| 289 |
<form id="cl-addons" method="post"> |
| 290 |
|
| 291 |
<?php |
| 292 |
|
| 293 |
if( !empty( $this->sections ) ){ |
| 294 |
foreach ( $this->sections as $section ){ |
| 295 |
echo $section; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 296 |
} |
| 297 |
} |
| 298 |
?> |
| 299 |
|
| 300 |
<!-- For plugins, we also need to ensure that the form posts back to our current page --> |
| 301 |
<input type="hidden" name="cl_all_add_ons" value="<?php echo esc_attr( implode( '|' ,$this->all_addons ) ); ?>" /> |
| 302 |
<input type="hidden" name="cl_all_plugins" value="<?php echo esc_attr( implode( '|' ,$this->all_plugins ) ); ?>" /> |
| 303 |
<input type="hidden" name="cl_add_ons_action" value="bulk_action" /> |
| 304 |
<?php wp_nonce_field('cl_add_ons_action'); ?> |
| 305 |
</form> |
| 306 |
</div> |
| 307 |
|
| 308 |
<?php |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
static function is_add_on_active( $slug ){ |
| 313 |
return apply_filters( 'cl_add_on_is_active', false, $slug ); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* process the actions for the Add-ons page |
| 319 |
*/ |
| 320 |
add_action( 'admin_init', 'cl_add_ons_listing_process_actions', 1 ); |
| 321 |
function cl_add_ons_listing_process_actions(){ |
| 322 |
if (current_user_can( 'manage_options' ) && isset( $_REQUEST['cl_add_ons_action'] ) && isset($_REQUEST['_wpnonce']) && wp_verify_nonce( sanitize_text_field( $_REQUEST['_wpnonce'] ), 'cl_add_ons_action' ) ){ |
| 323 |
|
| 324 |
if( $_REQUEST['cl_add_ons_action'] === 'bulk_action' ){ |
| 325 |
if( !empty( $_POST['cl_all_plugins'] ) && !empty( $_POST['cl_all_add_ons'] ) ){//make sure we have all the data |
| 326 |
//sanitize all data |
| 327 |
$all_plugins = explode( '|', sanitize_text_field( $_POST['cl_all_plugins'] ) ); |
| 328 |
$all_add_ons = explode( '|', sanitize_text_field( $_POST['cl_all_add_ons'] ) ); |
| 329 |
$plugins_to_activate = array(); |
| 330 |
if( !empty($_POST['cl_plugins']) && is_array($_POST['cl_plugins']) ){ |
| 331 |
$plugins_to_activate = array_map( 'sanitize_text_field', $_POST['cl_plugins'] ); |
| 332 |
} |
| 333 |
$add_ons_to_activate = array(); |
| 334 |
if( !empty($_POST['cl_add_ons']) && is_array($_POST['cl_add_ons']) ){ |
| 335 |
$add_ons_to_activate = array_map( 'sanitize_text_field', $_POST['cl_add_ons'] ); |
| 336 |
} |
| 337 |
|
| 338 |
foreach( $all_plugins as $plugin ){ |
| 339 |
if( in_array( $plugin, $plugins_to_activate ) ){ |
| 340 |
if( !is_plugin_active( $plugin ) ) { |
| 341 |
activate_plugin( $plugin ); |
| 342 |
} |
| 343 |
} |
| 344 |
else{ |
| 345 |
if( is_plugin_active( $plugin ) ) { |
| 346 |
deactivate_plugins( $plugin ); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
foreach( $all_add_ons as $add_on ){ |
| 352 |
if( in_array( $add_on, $add_ons_to_activate ) ){ |
| 353 |
do_action( 'cl_add_ons_activate', $add_on ); |
| 354 |
} |
| 355 |
else{ |
| 356 |
do_action( 'cl_add_ons_deactivate', $add_on ); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
} |
| 362 |
elseif ( $_REQUEST['cl_add_ons_action'] === 'activate' ){ |
| 363 |
if( !empty( $_REQUEST['cl_plugins'] ) ){//we have a plugin |
| 364 |
$plugin_slug = sanitize_text_field( $_REQUEST['cl_plugins'] ); |
| 365 |
if( !is_plugin_active( $plugin_slug ) ) { |
| 366 |
activate_plugin( $plugin_slug ); |
| 367 |
} |
| 368 |
} |
| 369 |
elseif( !empty( $_REQUEST['cl_add_ons'] ) ){//we have a add-on |
| 370 |
do_action( 'cl_add_ons_activate', sanitize_text_field($_REQUEST['cl_add_ons']) ); |
| 371 |
} |
| 372 |
} |
| 373 |
elseif ( $_REQUEST['cl_add_ons_action'] === 'deactivate' ){ |
| 374 |
if( !empty( $_REQUEST['cl_plugins'] ) ){//we have a plugin |
| 375 |
$plugin_slug = sanitize_text_field( $_REQUEST['cl_plugins'] ); |
| 376 |
if( is_plugin_active( $plugin_slug ) ) { |
| 377 |
deactivate_plugins( $plugin_slug ); |
| 378 |
} |
| 379 |
} |
| 380 |
elseif( !empty( $_REQUEST['cl_add_ons'] ) ){//we have a add-on |
| 381 |
do_action( 'cl_add_ons_deactivate', sanitize_text_field($_REQUEST['cl_add_ons']) ); |
| 382 |
} |
| 383 |
} |
| 384 |
if( isset( $_REQUEST['page'] ) ) |
| 385 |
wp_safe_redirect( add_query_arg( 'cl_add_ons_listing_success', 'true', admin_url( 'admin.php?page='. sanitize_text_field( $_REQUEST['page'] ) ) ) ); |
| 386 |
} |
| 387 |
} |
| 388 |
|