PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.6
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.6
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / assets / lib / cl-add-ons-listing / cl-add-ons-listing.php

cl-add-ons-listing.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.6, at assets/lib/cl-add-ons-listing/cl-add-ons-listing.php

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