PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.2
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.2
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 2.0.7 All 340 releases
profile-builder / admin / plugin-optin / class-plugin-optin.php

class-plugin-optin.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.2, at admin/plugin-optin/class-plugin-optin.php

1,024 lines 38.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 class Cozmoslabs_Plugin_Optin_WPPB {
6
7 public static $user_name = '';
8 public static $api_url = 'https://www.cozmoslabs.com/wp-json/cozmos-api/';
9 public static $base_url = 'https://usagetracker.cozmoslabs.com/update';
10 public static $plugin_optin_status = '';
11 public static $plugin_optin_email = '';
12
13 public static $plugin_option_key = 'cozmos_wppb_plugin_optin';
14 public static $plugin_option_email_key = 'cozmos_wppb_plugin_optin_email';
15
16 public function __construct(){
17
18 if( apply_filters( 'wppb_enable_plugin_optin', true ) === false )
19 return;
20
21 if ( !wp_next_scheduled( 'cozmos_wppb_plugin_optin_sync' ) )
22 wp_schedule_event( time(), 'weekly', 'cozmos_wppb_plugin_optin_sync' );
23
24 add_action( 'cozmos_wppb_plugin_optin_sync', array( 'Cozmoslabs_Plugin_Optin_WPPB', 'sync_data' ) );
25
26 self::$plugin_optin_status = get_option( self::$plugin_option_key, false );
27 self::$plugin_optin_email = get_option( self::$plugin_option_email_key, false );
28
29 add_action( 'admin_init', array( $this, 'redirect_to_plugin_optin_page' ) );
30 add_action( 'admin_menu', array( $this, 'add_submenu_page_optin' ) );
31 add_action( 'admin_init', array( $this, 'process_optin_actions' ) );
32 add_action( 'activate_plugin', array( $this, 'process_paid_plugin_activation' ) );
33 add_action( 'deactivated_plugin', array( $this, 'process_paid_plugin_deactivation' ) );
34 add_filter( 'wppb_advanced_settings_sanitize', array( $this, 'process_plugin_optin_advanced_setting' ), 20, 2 );
35
36 }
37
38 public function redirect_to_plugin_optin_page(){
39
40 if( ( isset( $_GET['page'] ) && sanitize_text_field( $_GET['page'] ) == 'wppb-optin-page' ) || ( isset( $_GET['page'] ) && isset( $_GET['subpage'] ) && sanitize_text_field( $_GET['page'] ) == 'profile-builder-dashboard' && sanitize_text_field( $_GET['subpage'] ) == 'wppb-setup' ) )
41 return;
42
43 if( self::$plugin_optin_status !== false )
44 return;
45
46 // Show this only when admin tries to access a plugin page
47 $target_slugs = array( 'profile-builder-', 'manage-fields', 'wppb-', 'admin-email-customizer', 'user-email-customizer', 'pbie', 'manage-fields', 'custom-redirects', 'pb-labels-edit' );
48 $is_plugin_page = false;
49
50 if( !empty( $target_slugs ) ){
51 foreach ( $target_slugs as $slug ){
52
53 if( ! empty( $_GET['page'] ) && false !== strpos( sanitize_text_field( $_GET['page'] ), $slug ) )
54 $is_plugin_page = true;
55
56 if( ! empty( $_GET['post_type'] ) && false !== strpos( sanitize_text_field( $_GET['post_type'] ), $slug ) )
57 $is_plugin_page = true;
58
59 if( ! empty( $_GET['post'] ) && false !== strpos( get_post_type( (int)$_GET['post'] ), $slug ) )
60 $is_plugin_page = true;
61
62 }
63 }
64
65 if( $is_plugin_page == true ){
66 wp_safe_redirect( admin_url( 'admin.php?page=wppb-optin-page' ) );
67 exit();
68 }
69
70 return;
71
72 }
73
74 public function add_submenu_page_optin() {
75 add_submenu_page( 'WPPBHidden', 'Profile Builder Plugin Optin', 'WPPBHidden', 'manage_options', 'wppb-optin-page', array(
76 $this,
77 'optin_page_content'
78 ) );
79 }
80
81 public function optin_page_content(){
82 require_once WPPB_PLUGIN_DIR . 'admin/plugin-optin/view-plugin-optin.php';
83 }
84
85 public function process_optin_actions(){
86
87 if( !isset( $_GET['page'] ) || $_GET['page'] != 'wppb-optin-page' || !isset( $_GET['_wpnonce'] ) )
88 return;
89
90 if( !current_user_can( 'manage_options' ) )
91 return;
92
93 if( wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'cozmos_enable_plugin_optin' ) ){
94
95 $args = array(
96 'method' => 'POST',
97 'body' => array(
98 'email' => get_option( 'admin_email' ),
99 'name' => self::get_user_name(),
100 'version' => self::get_current_active_version(),
101 'product' => 'wppb',
102 ),
103 );
104
105 // Check if the other plugin might be active as well
106 $args = $this->add_other_plugin_version_information( $args );
107
108 $request = wp_remote_post( self::$api_url . 'pluginOptinSubscribe/', $args );
109
110 update_option( self::$plugin_option_key, 'yes' );
111 update_option( self::$plugin_option_email_key, get_option( 'admin_email' ) );
112
113 $settings = get_option( 'wppb_toolbox_admin_settings', array() );
114
115 if( empty( $settings ) )
116 $settings = array( 'plugin-optin' => 'yes' );
117 else
118 $settings['plugin-optin'] = 'yes';
119
120 update_option( 'wppb_toolbox_admin_settings', $settings );
121
122 wp_safe_redirect( admin_url( 'admin.php?page=profile-builder-dashboard' ) );
123 exit;
124
125 }
126
127 if( wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'cozmos_disable_plugin_optin' ) ){
128
129 update_option( self::$plugin_option_key, 'no' );
130
131 $settings = get_option( 'wppb_toolbox_admin_settings', array() );
132
133 if( empty( $settings ) )
134 $settings = array( 'plugin-optin' => 'no' );
135 else
136 $settings['plugin-optin'] = 'no';
137
138 update_option( 'wppb_toolbox_admin_settings', $settings );
139
140 wp_safe_redirect( admin_url( 'admin.php?page=profile-builder-dashboard' ) );
141 exit;
142
143 }
144
145 }
146
147 // Update tags when a paid version is activated
148 public function process_paid_plugin_activation( $plugin ){
149
150 if( self::$plugin_optin_status !== 'yes' || self::$plugin_optin_email === false )
151 return;
152
153 $target_plugins = [ 'profile-builder-agency/index.php', 'profile-builder-pro/index.php', 'profile-builder-unlimited/index.php', 'profile-builder-hobbyist/index.php' ];
154
155 if( !in_array( $plugin, $target_plugins ) )
156 return;
157
158 $version = explode( '/', $plugin );
159 $version = str_replace( 'profile-builder-', '', $version[0] );
160
161 if( $version == 'hobbyist' )
162 $version == 'basic';
163
164 // Update user version tag
165 $args = array(
166 'method' => 'POST',
167 'body' => array(
168 'email' => self::$plugin_optin_email,
169 'version' => $version,
170 'product' => 'wppb',
171 )
172 );
173
174 // Check if the other plugin might be active as well
175 $args = $this->add_other_plugin_version_information( $args );
176
177 $request = wp_remote_post( self::$api_url . 'pluginOptinUpdateVersion/', $args );
178
179 }
180
181 // Update tags when a paid version is deactivated
182 public function process_paid_plugin_deactivation( $plugin ){
183
184 if( self::$plugin_optin_status !== 'yes' || self::$plugin_optin_email === false )
185 return;
186
187 $target_plugins = [ 'profile-builder-agency/index.php', 'profile-builder-pro/index.php', 'profile-builder-unlimited/index.php', 'profile-builder-hobbyist/index.php' ];
188
189 if( !in_array( $plugin, $target_plugins ) )
190 return;
191
192 // Update user version tag
193 $args = array(
194 'method' => 'POST',
195 'body' => [
196 'email' => self::$plugin_optin_email,
197 'version' => 'free',
198 'product' => 'wppb',
199 ],
200 );
201
202 $request = wp_remote_post( self::$api_url . 'pluginOptinUpdateVersion/', $args );
203
204 }
205
206 // Advanced settings
207 public function process_plugin_optin_advanced_setting( $settings, $previous_settings ){
208
209 if( !isset( $settings['plugin-optin'] ) || $settings['plugin-optin'] == 'no' ){
210
211 update_option( self::$plugin_option_key, 'no' );
212
213 if( self::$plugin_optin_email === false )
214 return $settings;
215
216 $args = array(
217 'method' => 'POST',
218 'body' => [
219 'email' => self::$plugin_optin_email,
220 'product' => 'wppb',
221 ],
222 );
223
224 $request = wp_remote_post( self::$api_url . 'pluginOptinArchiveSubscriber/', $args );
225
226 } else if ( $settings['plugin-optin'] == 'yes' ) {
227
228 if( isset( $previous_settings['plugin-optin'] ) && $settings['plugin-optin'] == $previous_settings['plugin-optin'] ){
229
230 // if the user has not changed the setting, we don't need to send the data again but if the option is not set, we need to send the data
231 if( self::$plugin_optin_status == 'yes' )
232 return $settings;
233
234 }
235
236 update_option( self::$plugin_option_key, 'yes' );
237 update_option( self::$plugin_option_email_key, get_option( 'admin_email' ) );
238
239 if( self::$plugin_optin_email === false )
240 return $settings;
241
242 $args = array(
243 'method' => 'POST',
244 'body' => [
245 'email' => self::$plugin_optin_email,
246 'name' => self::get_user_name(),
247 'product' => 'wppb',
248 'version' => self::get_current_active_version(),
249 ],
250 );
251
252 // Check if the other plugin might be active as well
253 $args = $this->add_other_plugin_version_information( $args );
254
255 $request = wp_remote_post( self::$api_url . 'pluginOptinSubscribe/', $args );
256
257 }
258
259 return $settings;
260
261 }
262
263 public function add_other_plugin_version_information( $args ){
264
265 $target_found = false;
266
267 // paid versions
268 $target_plugins = [ 'paid-member-subscriptions-agency/index.php', 'paid-member-subscriptions-pro/index.php', 'paid-member-subscriptions-unlimited/index.php', 'paid-member-subscriptions-basic/index.php' ];
269
270 foreach( $target_plugins as $plugin ){
271 if( is_plugin_active( $plugin ) || is_plugin_active_for_network( $plugin ) ){
272 $target_found = $plugin;
273 break;
274 }
275 }
276
277 // verify free version separately
278 if( $target_found === false ){
279
280 if( is_plugin_active( 'paid-member-subscriptions/index.php' ) || is_plugin_active_for_network( 'paid-member-subscriptions/index.php' ) )
281 $target_found = 'paid-member-subscriptions-free';
282
283 }
284
285 if( $target_found !== false ){
286
287 $target_found = explode( '/', $target_found );
288 $target_found = str_replace( 'paid-member-subscriptions-', '', $target_found[0] );
289
290 $args['body']['other_product_data'] = array(
291 'product' => 'pms',
292 'version' => $target_found,
293 );
294
295 }
296
297 return $args;
298
299 }
300
301 // Determine current user name
302 public static function get_user_name(){
303
304 if( !empty( self::$user_name ) )
305 return self::$user_name;
306
307 $user = wp_get_current_user();
308
309 $name = $user->display_name;
310
311 $first_name = get_user_meta( $user->ID, 'first_name', true );
312 $last_name = get_user_meta( $user->ID, 'last_name', true );
313
314 if( !empty( $first_name ) && !empty( $last_name ) )
315 $name = $first_name . ' ' . $last_name;
316
317 self::$user_name = $name;
318
319 return self::$user_name;
320
321 }
322
323 // Determine current active plugin version
324 public static function get_current_active_version(){
325
326 if( !function_exists( 'is_plugin_active' ) || !function_exists( 'is_plugin_active_for_network' ) )
327 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
328
329 if( is_plugin_active( 'profile-builder-agency/index.php' ) || is_plugin_active_for_network( 'profile-builder-agency/index.php' ) )
330 return 'agency';
331 elseif( is_plugin_active( 'profile-builder-pro/index.php' ) || is_plugin_active_for_network( 'profile-builder-pro/index.php' ) )
332 return 'pro';
333 elseif( is_plugin_active( 'profile-builder-unlimited/index.php' ) || is_plugin_active_for_network( 'profile-builder-unlimited/index.php' ) )
334 return 'unlimited';
335 elseif( is_plugin_active( 'profile-builder-hobbyist/index.php' ) || is_plugin_active_for_network( 'profile-builder-hobbyist/index.php' ) )
336 return 'basic';
337
338 return 'free';
339
340 }
341
342 public static function sync_data(){
343
344 if( self::$plugin_optin_status != 'yes' )
345 return;
346
347 $args = array(
348 'method' => 'POST',
349 'body' => array(
350 'home_url' => home_url(),
351 'product' => 'wppb',
352 'email' => self::$plugin_optin_email,
353 'name' => self::get_user_name(),
354 'version' => self::get_current_active_version(),
355 'license' => wppb_get_serial_number(),
356 'active_plugins' => json_encode( get_option( 'active_plugins', array() ) ),
357 'wp_version' => get_bloginfo('version'),
358 'wp_locale' => get_locale(),
359 'plugin_version' => defined( 'PROFILE_BUILDER_VERSION' ) ? PROFILE_BUILDER_VERSION : '',
360 'php_version' => defined( 'PHP_VERSION' ) ? PHP_VERSION : '',
361 ),
362 );
363
364 // Only send the major version for WordPress and PHP
365 // e.g. 1.x
366 $target_keys = array( 'wp_version', 'php_version' );
367
368 foreach( $target_keys as $key ){
369 $version_number = explode( '.', $args['body'][$key] );
370
371 if( isset( $version_number[0] ) && isset( $version_number[1] ) )
372 $args['body'][$key] = $version_number[0] . '.' . $version_number[1];
373 }
374
375 $args = apply_filters( 'cozmoslabs_plugin_optin_wppb_metadata', $args );
376
377 $request = wp_remote_post( self::$base_url, $args );
378
379 // echo wp_remote_retrieve_body( $request );
380 // die();
381
382 }
383
384 }
385
386 new Cozmoslabs_Plugin_Optin_WPPB();
387
388 if( !class_exists( 'Cozmoslabs_Plugin_Optin_Metadata_Builder' ) ) {
389 class Cozmoslabs_Plugin_Optin_Metadata_Builder {
390
391 public $option_prefix = '';
392 public $blacklisted_option_slugs = [];
393 public $blacklisted_option_patterns = [];
394 public $blacklisted_option_names = [];
395 protected $metadata;
396
397 public function __construct(){
398
399 $this->metadata = [
400 'settings' => [],
401 'add-ons' => [],
402 'custom' => [],
403 'cpt' => [],
404 ];
405
406 add_filter( 'cozmoslabs_plugin_optin_'. $this->option_prefix .'metadata', array( $this, 'build_metadata' ) );
407
408 }
409
410 public function build_metadata( $args ){
411 // Get all options that start with the prefix
412 $options = $this->get_option_keys();
413
414 if( !empty( $options ) ){
415
416 foreach( $options as $option ){
417
418 // exclude exact option names
419 if( in_array( $option['option_name'], $this->blacklisted_option_slugs ) ){
420 continue;
421 }
422
423 // exclude patterns
424 if( !empty( $this->blacklisted_option_patterns ) ){
425 $found_pattern = false;
426
427 foreach( $this->blacklisted_option_patterns as $pattern ){
428 if( strpos( $option['option_name'], $pattern ) !== false ){
429 $found_pattern = true;
430 break;
431 }
432 }
433
434 if( $found_pattern )
435 continue;
436 }
437
438 $option_value = get_option( $option['option_name'], false );
439
440 if( !empty( $option_value ) ){
441
442 if( is_array( $option_value ) ){
443 foreach( $option_value as $key => $value ){
444 if( !is_array( $value ) ){
445 if( in_array( $key, $this->blacklisted_option_names ) )
446 unset( $option_value[ $key ] );
447 } else {
448 if( in_array( $key, $this->blacklisted_option_names ) )
449 unset( $option_value[ $key ] );
450
451 foreach( $value as $key_deep => $value_deep ){
452 if( in_array( $key_deep, $this->blacklisted_option_names ) )
453 unset( $option_value[ $key ][ $key_deep ] );
454 }
455 }
456 }
457 }
458
459 // cleanup options like array( array( 'abc' ) ) to be array( 'abc' )
460 if( is_array( $option_value ) && count( $option_value ) == 1 && isset( $option_value[0] ) )
461 $option_value = $option_value[0];
462
463 $this->metadata['settings'][ $option['option_name'] ] = $option_value;
464 }
465
466 }
467
468 }
469
470 // Ability to add custom data
471 $this->metadata = apply_filters( 'cozmoslabs_plugin_optin_'. $this->option_prefix .'metadata_builder_metadata', $this->metadata );
472
473 $args['body']['metadata'] = $this->metadata;
474
475 return $args;
476 }
477
478 private function get_option_keys(){
479
480 global $wpdb;
481
482 if( empty( $this->option_prefix ) )
483 return [];
484
485 $result = $wpdb->get_results( $wpdb->prepare( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE %s", $this->option_prefix . '%' ), 'ARRAY_A' );
486
487 if( !empty( $result ) )
488 return $result;
489
490 return [];
491
492 }
493
494 }
495 }
496
497 class Cozmoslabs_Plugin_Optin_Metadata_Builder_WPPB extends Cozmoslabs_Plugin_Optin_Metadata_Builder {
498
499 public function __construct(){
500
501 $this->option_prefix = 'wppb_';
502
503 parent::__construct();
504
505 $this->blacklisted_option_slugs = [
506 'wppb-single-ul-templates',
507 'wppb-ul-templates',
508 'wppb_admin_emailc_common_settings_header',
509 'wppb_admin_emailc_default_registration_email_content',
510 'wppb_admin_emailc_default_registration_email_enabled',
511 'wppb_admin_emailc_default_registration_email_subject',
512 'wppb_admin_emailc_epaa_notification_content',
513 'wppb_admin_emailc_epaa_notification_enabled',
514 'wppb_admin_emailc_epaa_notification_subject',
515 'wppb_admin_emailc_registration_with_admin_approval_email_content',
516 'wppb_admin_emailc_registration_with_admin_approval_email_enabled',
517 'wppb_admin_emailc_registration_with_admin_approval_email_subject',
518 'wppb_admin_emailc_user_password_reset_email_content',
519 'wppb_admin_emailc_user_password_reset_email_enabled',
520 'wppb_admin_emailc_user_password_reset_email_subject',
521 'wppb_advanced_add_ons_settings',
522 'wppb_cleaned_up_user_status_taxonomy_from_db',
523 'wppb_cmi_api_key_validated',
524 'wppb_cr_default_wp_pages', // CR settings are added in a special way below
525 'wppb_cr_global',
526 'wppb_cr_role',
527 'wppb_cr_user',
528 'wppb_display_admin_settings',
529 'wppb_edd_sl_initial_activation',
530 'wppb_emailc_common_settings_from_name',
531 'wppb_emailc_common_settings_from_reply_to_email',
532 'wppb_free_add_ons_settings',
533 'wppb_license_details',
534 'wppb_license_key',
535 'wppb_license_status',
536 'wppb_mailchimp_api_key_validated',
537 'wppb_manage_fields',
538 'wppb_module_settings',
539 'wppb_module_settings_description',
540 'wppb_msf_break_points',
541 'wppb_msf_tab_titles',
542 'wppb_old_add_ons_status',
543 'wppb_pages_created',
544 'wppb_profile_builder_hobbyist_serial_status',
545 'wppb_profile_builder_pro_serial',
546 'wppb_profile_builder_pro_serial_status',
547 'wppb_repackage_initial_upgrade',
548 'wppb_review_request_status',
549 'wppb_roles_editor_capabilities',
550 'wppb_setup_wizard_steps',
551 'wppb_user_emailc_admin_approval_notif_approved_email_content',
552 'wppb_user_emailc_admin_approval_notif_approved_email_enabled',
553 'wppb_user_emailc_admin_approval_notif_approved_email_subject',
554 'wppb_user_emailc_admin_approval_notif_unapproved_email_content',
555 'wppb_user_emailc_admin_approval_notif_unapproved_email_enabled',
556 'wppb_user_emailc_admin_approval_notif_unapproved_email_subject',
557 'wppb_user_emailc_change_email_address_content',
558 'wppb_user_emailc_change_email_address_enabled',
559 'wppb_user_emailc_change_email_address_request_content',
560 'wppb_user_emailc_change_email_address_request_enabled',
561 'wppb_user_emailc_change_email_address_request_subject',
562 'wppb_user_emailc_change_email_address_subject',
563 'wppb_user_emailc_default_registration_email_content',
564 'wppb_user_emailc_default_registration_email_enabled',
565 'wppb_user_emailc_default_registration_email_subject',
566 'wppb_user_emailc_epaa_notification_content',
567 'wppb_user_emailc_epaa_notification_enabled',
568 'wppb_user_emailc_epaa_notification_subject',
569 'wppb_user_emailc_registration_with_admin_approval_email_content',
570 'wppb_user_emailc_registration_with_admin_approval_email_enabled',
571 'wppb_user_emailc_registration_with_admin_approval_email_subject',
572 'wppb_user_emailc_registr_w_email_confirm_email_content',
573 'wppb_user_emailc_registr_w_email_confirm_email_enabled',
574 'wppb_user_emailc_registr_w_email_confirm_email_subject',
575 'wppb_user_emailc_reset_email_content',
576 'wppb_user_emailc_reset_email_enabled',
577 'wppb_user_emailc_reset_email_subject',
578 'wppb_user_emailc_reset_success_email_content',
579 'wppb_user_emailc_reset_success_email_enabled',
580 'wppb_user_emailc_reset_success_email_subject',
581 'wppb_user_pages',
582 'wppb_version',
583 'wppb_recaptcha_validations',
584 'wppb_turnstile_validations',
585 ];
586
587 $this->blacklisted_option_names = [
588 'api_key',
589 'facebook-app-id',
590 'google-client-id',
591 'google-client-name',
592 'twitter-api-key',
593 'twitter-api-secret',
594 'linkedin-client-id',
595 'linkedin-client-secret',
596 'admin-emails',
597 'allowed_pages',
598 'allowed_paths',
599 'allowed_query_strings',
600 'redirect_to',
601 'lists',
602 'heading-before-reg-buttons',
603 'heading-before-ep-buttons',
604 'facebook-button-text',
605 'google-button-text',
606 'twitter-button-text',
607 'linkedin-button-text',
608 'facebook-button-text-ep',
609 'google-button-text-ep',
610 'twitter-button-text-ep',
611 'linkedin-button-text-ep',
612 'message_logged_out',
613 'message_logged_in',
614 'purchasing_restricted'
615 ];
616
617 $this->blacklisted_option_patterns = [
618 'wppb_repeater',
619 ];
620
621 add_action( 'cozmoslabs_plugin_optin_'. $this->option_prefix .'metadata_builder_metadata', array( $this, 'build_custom_plugin_metadata' ) );
622
623 }
624
625 public function build_custom_plugin_metadata(){
626
627 // add custom redirects settings inside the settings array
628 $this->generate_custom_redirects_settings();
629
630 // content restriction data first
631 $this->metadata['custom']['content_restriction'] = $this->generate_content_restriction_data();
632
633 // add-ons data
634 $this->metadata['addons'] = $this->generate_addon_settings();
635
636 // custom fields data
637 $this->metadata['custom']['custom-fields'] = $this->generate_custom_fields_data();
638
639 // custom post types data
640 $this->metadata['cpt'] = $this->generate_cpt_data();
641
642 return $this->metadata;
643
644 }
645
646 public function generate_custom_redirects_settings(){
647
648 $custom_redirects_option_slugs = [
649 'wppb_cr_user',
650 'wppb_cr_default_wp_pages',
651 'wppb_cr_global',
652 'wppb_cr_role',
653 ];
654
655 $custom_redirects_data = [];
656
657 foreach( $custom_redirects_option_slugs as $option_slug ){
658 $option = get_option( $option_slug, false );
659
660 $normalized_option_slug = str_replace( 'wppb_cr_', '', $option_slug );
661
662 if( !empty( $option ) ){
663 foreach( $option as $slug => $value ){
664 if( !isset( $custom_redirects_data[ $normalized_option_slug ] ) )
665 $custom_redirects_data[ $normalized_option_slug ] = array();
666
667 $custom_redirects_data[ $normalized_option_slug ][] = $value['type'];
668 }
669 }
670 }
671
672 foreach( $custom_redirects_data as $key => $entry ){
673 if( is_array( $entry ) )
674 $custom_redirects_data[ $key ] = implode( ',', $entry );
675 }
676
677 $this->metadata['settings']['wppb_custom_redirects_settings'] = $custom_redirects_data;
678
679 }
680
681 /**
682 * Using this to normalize all of the different ways add-ons status is stored
683 */
684 public function generate_addon_settings(){
685 $add_on_option_slugs = [
686 'wppb_free_add_ons_settings',
687 'wppb_advanced_add_ons_settings',
688 'wppb_module_settings',
689 ];
690
691 $name_normalization = [
692 'userListing' => 'user-listing',
693 'customRedirect' => 'custom-redirects',
694 'multipleEditProfileForms' => 'multiple-edit-profile-forms',
695 'multipleRegistrationForms' => 'multiple-registration-forms',
696 'repeaterFields' => 'repeater-fields',
697 'fileRestriction' => 'files-restriction',
698 ];
699
700 $add_ons = [];
701
702 foreach( $add_on_option_slugs as $option_slug ){
703 $option = get_option( $option_slug, false );
704
705 if( !empty( $option ) ){
706 foreach( $option as $slug => $value ){
707
708 if( ( is_bool( $value ) && $value == true ) || $value == 'show' ){
709 $slug = str_replace( 'wppb_', '', $slug );
710
711 if( isset( $name_normalization[$slug] ) )
712 $slug = $name_normalization[$slug];
713
714 $add_ons[ str_replace( 'wppb_', '', $slug ) ] = true;
715 }
716 }
717 }
718 }
719
720 // Email Customizer was moved to a free add-on at some point, remove it from the array if we find it
721 if( isset( $add_ons['emailCustomizer'] ) )
722 unset( $add_ons['emailCustomizer'] );
723
724 if( isset( $add_ons['emailCustomizerAdmin'] ) )
725 unset( $add_ons['emailCustomizerAdmin'] );
726
727 // Add content restriction integrations as active add-ons if they have restrictions
728 if( !empty( $this->metadata['custom']['content_restriction'] ) ) {
729 // Elementor integration
730 if( !empty( $this->metadata['custom']['content_restriction']['elementor_restrictions'] ) ) {
731 $add_ons['elementor-integration'] = true;
732 }
733
734 // Gutenberg integration
735 if( !empty( $this->metadata['custom']['content_restriction']['blocks_restrictions'] ) ) {
736 $add_ons['gutenberg-integration'] = true;
737 }
738
739 // Divi integration
740 if( !empty( $this->metadata['custom']['content_restriction']['divi_restrictions'] ) ) {
741 $add_ons['divi-integration'] = true;
742 }
743
744 // WooCommerce integration
745 if( !empty( $this->metadata['custom']['content_restriction']['woocommerce_restrictions'] ) &&
746 ( !empty( $this->metadata['custom']['content_restriction']['woocommerce_restrictions']['view'] ) ||
747 !empty( $this->metadata['custom']['content_restriction']['woocommerce_restrictions']['purchase'] ) )
748 ) {
749 $add_ons['woocommerce-integration'] = true;
750 }
751 }
752
753 return $add_ons;
754 }
755
756 public function generate_custom_fields_data(){
757
758 $manage_fields = get_option( 'wppb_manage_fields', false );
759
760 if( empty( $manage_fields ) )
761 return '';
762
763 $whitelisted_settings = [
764 'field' => '',
765 'required' => '',
766 'overwrite-existing' => '',
767 'conditional-logic-enabled' => '',
768 'conditional-logic' => '',
769 'edit-profile-approved-by-admin' => '',
770 'visibility' => '',
771 'user-role-visibility' => '',
772 'location-visibility' => '',
773 'simple-upload' => '',
774 'recaptcha-type' => '',
775 'captcha-pb-forms' => '',
776 'captcha-wp-forms' => '',
777 ];
778
779 $custom_fields = [];
780
781 foreach( $manage_fields as $field ){
782
783 // Fix for recaptcha & turnstile settings being present on every field
784 if( $field['field'] != 'reCAPTCHA' ){
785 unset( $field['recaptcha-type'] );
786 unset( $field['captcha-pb-forms'] );
787 unset( $field['captcha-wp-forms'] );
788 }
789 if( $field['field'] != 'Turnstile' ){
790 unset( $field['theme'] );
791 unset( $field['turnstile-site-key'] );
792 unset( $field['turnstile-secret-key'] );
793 unset( $field['turnstile-pb-forms'] );
794 unset( $field['turnstile-wp-forms'] );
795 }
796
797 $custom_fields[] = array_filter( array_intersect_key( $field, $whitelisted_settings ) );
798
799 }
800
801 return $custom_fields;
802
803 }
804
805 public function generate_cpt_data(){
806
807 // Userlistings
808 $userlistings = get_posts(
809 array( 'posts_per_page' => -1, 'post_status' =>'publish', 'post_type' => 'wppb-ul-cpt', 'orderby' => 'post_date', 'order' => 'ASC', 'fields' => 'ID' )
810 );
811
812 $cpt_data = [];
813
814 if( !empty( $userlistings ) ){
815 foreach( $userlistings as $userlisting ){
816
817 $data = get_post_meta( $userlisting->ID, 'wppb_ul_page_settings', true );
818
819 if( !empty( $data[0] ) )
820 $data = $data[0];
821 else
822 $data = [];
823
824 $faceted_settings = get_post_meta( $userlisting->ID, 'wppb_ul_faceted_settings', true );
825
826 if( !empty( $faceted_settings ) )
827 $data['facets'] = $faceted_settings;
828
829 $active_ul_theme = get_post_meta( $userlisting->ID, 'wppb-ul-active-theme', true );
830
831 if( !empty( $active_ul_theme ) )
832 $data['theme'] = $active_ul_theme;
833
834 $cpt_data['userlistings'] = $data;
835
836 }
837 }
838
839 // Registration Forms
840 $registration_forms = get_posts(
841 array( 'posts_per_page' => -1, 'post_status' => 'publish' , 'post_type' => 'wppb-rf-cpt', 'fields' => 'ID' )
842 );
843
844 if( !empty( $registration_forms ) ){
845 foreach( $registration_forms as $form ) {
846
847 $data = get_post_meta( $form->ID, 'wppb_rf_page_settings', true );
848
849 if( !empty( $data[0] ) )
850 $data = $data[0];
851 else
852 $data = [];
853
854 $form_fields = get_post_meta( $form->ID, 'wppb_rf_fields', true );
855
856 if( !empty( $form_fields ) )
857 $data['fields'] = $form_fields;
858
859 $form_social_connect_active = get_post_meta( $form->ID, 'wppb_sc_rf_epf_active', true );
860
861 if( !empty( $form_social_connect_active ) )
862 $data['social_connect_active'] = $form_social_connect_active;
863
864 $form_msf_settings = get_post_meta( $form->ID, 'wppb_msf_post_options', true );
865
866 if( !empty( $form_msf_settings ) )
867 $data['msf_settings'] = $form_msf_settings;
868
869 $cpt_data['rf'] = $data;
870 }
871 }
872
873 // Edit Profile Forms
874 $edit_profile_forms = get_posts(
875 array( 'posts_per_page' => -1, 'post_status' => 'publish' , 'post_type' => 'wppb-epf-cpt', 'fields' => 'ID' )
876 );
877
878 if( !empty( $edit_profile_forms ) ){
879 foreach( $edit_profile_forms as $form ) {
880
881 $data = get_post_meta( $form->ID, 'wppb_epf_page_settings', true );
882
883 if( !empty( $data[0] ) )
884 $data = $data[0];
885 else
886 $data = [];
887
888 $form_fields = get_post_meta( $form->ID, 'wppb_epf_fields', true );
889
890 if( !empty( $form_fields ) )
891 $data['fields'] = $form_fields;
892
893 $form_social_connect_active = get_post_meta( $form->ID, 'wppb_sc_rf_epf_active', true );
894
895 if( !empty( $form_social_connect_active ) )
896 $data['social_connect_active'] = $form_social_connect_active;
897
898 $form_msf_settings = get_post_meta( $form->ID, 'wppb_msf_post_options', true );
899
900 if( !empty( $form_msf_settings ) )
901 $data['msf_settings'] = $form_msf_settings;
902
903 $cpt_data['epf'] = $data;
904 }
905 }
906
907 return $cpt_data;
908
909 }
910
911 public function generate_content_restriction_data(){
912
913 $restriction_data = [
914 'post_restrictions' => 0,
915 'elementor_restrictions' => 0,
916 'divi_restrictions' => 0,
917 'blocks_restrictions' => 0,
918 'woocommerce_restrictions' => [
919 'view' => 0,
920 'purchase' => 0
921 ]
922 ];
923
924 // Count post/page/cpt restrictions
925 global $wpdb;
926 $restriction_data['post_restrictions'] = $wpdb->get_var(
927 "SELECT COUNT(DISTINCT a.post_id)
928 FROM {$wpdb->postmeta} a
929 INNER JOIN {$wpdb->posts} b ON a.post_id = b.ID
930 WHERE b.post_type != 'revision'
931 AND ( ( a.meta_key = 'wppb-content-restrict-user-status' AND a.meta_value = 'loggedin' )
932 OR ( a.meta_key = 'wppb-content-restrict-user-role' AND a.meta_value IS NOT NULL ) ) LIMIT 100"
933 );
934
935
936 // Count Elementor widget restrictions if Elementor is active
937 if( did_action( 'elementor/loaded' ) ) {
938
939 /**
940 * Searching for:
941 *
942 * "wppb_restriction_loggedin_users":"yes"
943 * "wppb_restriction_loggedout_users":"yes"
944 * "wppb_restriction_user_roles":["
945 */
946 $elementor_posts = $wpdb->get_results(
947 "SELECT a.post_id, a.meta_value
948 FROM {$wpdb->postmeta} a
949 INNER JOIN {$wpdb->posts} b ON a.post_id = b.ID
950 WHERE b.post_type != 'revision'
951 AND a.meta_key = '_elementor_data'
952 AND ( a.meta_value LIKE '%\"wppb_restriction_loggedin_users\":\"yes\"%'
953 OR a.meta_value LIKE '%\"wppb_restriction_loggedout_users\":\"yes\"%'
954 OR a.meta_value LIKE '%\"wppb_restriction_user_roles\":[\"%') LIMIT 100"
955 );
956
957 if( !empty( $elementor_posts ) )
958 $restriction_data['elementor_restrictions'] = count($elementor_posts);
959
960 }
961
962 // Check if Divi is active (either theme or plugin)
963 if( defined( 'ET_BUILDER_VERSION' ) ) {
964 // Count Divi builder restrictions
965 $divi_posts = $wpdb->get_results(
966 "SELECT ID
967 FROM {$wpdb->posts}
968 WHERE post_type != 'revision'
969 AND post_content LIKE '%wppb_display_to=\"logged_in\"%'
970 OR post_content LIKE '%wppb_display_to=\"not_logged_in\"%' LIMIT 100"
971 );
972
973 if( !empty( $divi_posts ) )
974 $restriction_data['divi_restrictions'] = count($divi_posts);
975
976 }
977
978 // Check if Gutenberg is available
979 if( version_compare( get_bloginfo( 'version' ), '5.0', '>=' ) ) {
980 // Count Gutenberg restrictions
981 $gutenberg_posts = $wpdb->get_results(
982 "SELECT ID
983 FROM {$wpdb->posts}
984 WHERE post_type != 'revision'
985 AND ( post_content LIKE '%\"display_to\":\"\",\"enable_message_logged_in\"%'
986 OR post_content LIKE '%\"display_to\":\"not_logged_in\",\"enable_message_logged_in\"%'
987 OR post_content LIKE '%\"wppbContentRestriction\":{\"user_roles\":[\"%' ) LIMIT 100"
988 );
989
990 if( !empty( $gutenberg_posts ) )
991 $restriction_data['blocks_restrictions'] = count($gutenberg_posts);
992
993 }
994
995 // Count WooCommerce restrictions if WooCommerce is active
996 if( class_exists( 'WooCommerce' ) ) {
997 // Count view restrictions
998 $restriction_data['woocommerce_restrictions']['view'] = $wpdb->get_var(
999 "SELECT COUNT(DISTINCT a.post_id)
1000 FROM {$wpdb->postmeta} a
1001 INNER JOIN {$wpdb->posts} b ON a.post_id = b.ID
1002 WHERE b.post_type = 'product'
1003 AND ( ( a.meta_key = 'wppb-content-restrict-user-status' AND a.meta_value = 'loggedin' )
1004 OR ( a.meta_key = 'wppb-content-restrict-user-role' AND a.meta_value IS NOT NULL ) ) LIMIT 100"
1005 );
1006
1007 // Count purchase restrictions
1008 $restriction_data['woocommerce_restrictions']['purchase'] = $wpdb->get_var(
1009 "SELECT COUNT(DISTINCT a.post_id)
1010 FROM {$wpdb->postmeta} a
1011 INNER JOIN {$wpdb->posts} b ON a.post_id = b.ID
1012 WHERE b.post_type = 'product'
1013 AND ( ( a.meta_key = 'wppb-purchase-restrict-user-status' AND a.meta_value = 'loggedin' )
1014 OR ( a.meta_key = 'wppb-purchase-restrict-user-role' AND a.meta_value IS NOT NULL ) ) LIMIT 100"
1015 );
1016 }
1017
1018 return $restriction_data;
1019
1020 }
1021
1022 }
1023
1024 new Cozmoslabs_Plugin_Optin_Metadata_Builder_WPPB();