| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Description: Enables editing of labels from Profile Builder. |
| 5 |
*/ |
| 6 |
|
| 7 |
|
| 8 |
/* function that enqueues the necessary scripts */ |
| 9 |
function wppb_le_scripts_and_styles( $hook ) { |
| 10 |
if( $hook == 'profile-builder_page_pb-labels-edit' ) { |
| 11 |
wp_enqueue_script( 'pble_init', plugin_dir_url( __FILE__ ) . 'assets/js/init.js', array( 'jquery' ) ); |
| 12 |
wp_enqueue_script( 'pble_chosen', plugin_dir_url( __FILE__ ) . 'assets/chosen/chosen.jquery.min.js', array( 'jquery' ) ); |
| 13 |
wp_enqueue_style( 'pble_chosen_css', plugin_dir_url( __FILE__ ) . 'assets/chosen/chosen.css' ); |
| 14 |
wp_enqueue_style( 'pble_css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css' ); |
| 15 |
} |
| 16 |
} |
| 17 |
add_action( 'admin_enqueue_scripts', 'wppb_le_scripts_and_styles' ); |
| 18 |
|
| 19 |
/* load required files */ |
| 20 |
require_once 'potx.php'; |
| 21 |
require_once 'inc/class-pble-import.php'; |
| 22 |
require_once 'inc/class-pble-export.php'; |
| 23 |
//require_once 'assets/lib/wck-api/wordpress-creation-kit.php'; |
| 24 |
|
| 25 |
/* scan labels on plugin activate if not already scanned */ |
| 26 |
function wppb_le_scan_on_plugin_activate() { |
| 27 |
$pble_check = get_option( 'pble_backup', 'not_set' ); |
| 28 |
|
| 29 |
if( empty( $pble_check ) || $pble_check === 'not_set' ) { |
| 30 |
// use of output buffer to fix "headers already sent" notice on plugin activation |
| 31 |
ob_start(); |
| 32 |
wppb_le_scan_labels( wp_create_nonce( 'wppb_rescan_labels' ) ); |
| 33 |
$output = ob_get_clean(); |
| 34 |
} |
| 35 |
} |
| 36 |
register_activation_hook( __FILE__, 'wppb_le_scan_on_plugin_activate' ); |
| 37 |
|
| 38 |
/* scan pble labels */ |
| 39 |
function wppb_le_scan_labels( $nonce ) { |
| 40 |
|
| 41 |
if( !wp_verify_nonce( $nonce, 'wppb_rescan_labels' ) ) |
| 42 |
return; |
| 43 |
|
| 44 |
// create directory iterator |
| 45 |
$ite = new RecursiveDirectoryIterator( WPPB_PLUGIN_DIR ); |
| 46 |
|
| 47 |
// array with files to get strings from |
| 48 |
$pb_files_to_get = apply_filters( 'pb_files_to_get', |
| 49 |
array( |
| 50 |
'functions.php', |
| 51 |
'login.php', |
| 52 |
'recover.php', |
| 53 |
'register.php', |
| 54 |
'logout.php', |
| 55 |
'class-formbuilder.php', |
| 56 |
'edit-profile.php', |
| 57 |
'admin-approval.php', |
| 58 |
'email-confirmation.php', |
| 59 |
'userlisting.php', |
| 60 |
'email.php', |
| 61 |
'username.php', |
| 62 |
'password-repeat.php', |
| 63 |
'form-designs.php', |
| 64 |
'profile-builder.catalog.php' |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
global $wppb_strings; |
| 69 |
$wppb_strings = array(); |
| 70 |
|
| 71 |
// loop through directory and get _e() and __() function calls |
| 72 |
foreach( new RecursiveIteratorIterator( $ite ) as $filename => $current_file ) { |
| 73 |
// http://php.net/manual/en/class.splfileinfo.php |
| 74 |
if( isset( $current_file ) ) { |
| 75 |
$current_file_pathinfo = pathinfo( $current_file ); |
| 76 |
if( isset( $current_file_pathinfo['extension'] ) ) { |
| 77 |
if( ! empty( $current_file_pathinfo['extension'] ) && $current_file_pathinfo['extension'] == "php" ) { |
| 78 |
if( in_array( basename( $current_file ), $pb_files_to_get ) ) { |
| 79 |
if( file_exists( $current_file ) ) { |
| 80 |
_wppb_le_potx_process_file( realpath( $current_file ), 0, '_wppb_le_output_str2' ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
update_option( 'pble_backup', $wppb_strings ); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
// populate array with Profile Builder labels |
| 93 |
function _wppb_le_output_str2( $str ) { |
| 94 |
global $wppb_strings; |
| 95 |
if( is_array( $wppb_strings ) && ! in_array( $str, $wppb_strings ) ) { |
| 96 |
$wppb_strings[] = $str; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/* scan pble labels on Rescan button click */ |
| 101 |
function wppb_le_rescan() { |
| 102 |
|
| 103 |
if( isset( $_POST['rescan'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_rescan_labels' ) ) { |
| 104 |
wppb_le_scan_labels( sanitize_text_field( $_POST['wppb_nonce'] ) ); |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
add_action( 'init', 'wppb_le_rescan' ); |
| 109 |
|
| 110 |
/* rescan success message */ |
| 111 |
function wppb_le_rescan_success_message() { |
| 112 |
if( isset( $_POST['rescan'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_rescan_labels' ) ) { |
| 113 |
global $wppb_strings; |
| 114 |
$wppb_strings_count = count( $wppb_strings ); |
| 115 |
|
| 116 |
$rescan_message = '<div id="message" class="updated"><p>' . $wppb_strings_count . __(' labels scanned.', 'profile-builder') . '</p></div>'; |
| 117 |
echo wp_kses_post( $rescan_message ); |
| 118 |
} |
| 119 |
} |
| 120 |
add_action( 'admin_notices', 'wppb_le_rescan_success_message' ); |
| 121 |
|
| 122 |
/* |
| 123 |
* change text strings |
| 124 |
* |
| 125 |
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext |
| 126 |
*/ |
| 127 |
function wppb_le_text_strings( $translated_text, $text, $domain ) { |
| 128 |
if( is_admin() ) |
| 129 |
return $translated_text; |
| 130 |
|
| 131 |
$edited_labels = get_option( 'pble' ); |
| 132 |
|
| 133 |
if( empty( $edited_labels ) || $edited_labels === 'not_set' ) { |
| 134 |
return $translated_text; |
| 135 |
} |
| 136 |
|
| 137 |
if( is_array( $edited_labels ) && ! empty( $edited_labels ) ) { |
| 138 |
foreach( $edited_labels as $inner_array ) { |
| 139 |
if( $text === $inner_array['pble-label'] || $text === htmlentities($inner_array['pble-label']) ) { |
| 140 |
$translated_text = wp_kses_post( $inner_array['pble-newlabel'] ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $translated_text; |
| 146 |
} |
| 147 |
add_filter( 'gettext', 'wppb_le_text_strings', 8, 3 ); |
| 148 |
|
| 149 |
function wppb_le_ngettext_strings( $translated_text, $single, $plural, $number, $domain ){ |
| 150 |
if( is_admin() ) |
| 151 |
return $translated_text; |
| 152 |
|
| 153 |
if( $domain != 'profile-builder' ) |
| 154 |
return $translated_text; |
| 155 |
|
| 156 |
$edited_labels = get_option( 'pble' ); |
| 157 |
|
| 158 |
if( empty( $edited_labels ) || $edited_labels === 'not_set' ) { |
| 159 |
return $translated_text; |
| 160 |
} |
| 161 |
|
| 162 |
if( is_array( $edited_labels ) && ! empty( $edited_labels ) ) { |
| 163 |
foreach( $edited_labels as $inner_array ) { |
| 164 |
if( $single === $inner_array['pble-label'] ) { |
| 165 |
$translated_text = wp_kses_post( $inner_array['pble-newlabel'] ); |
| 166 |
} |
| 167 |
if( $plural === $inner_array['pble-label'] ) { |
| 168 |
$translated_text = wp_kses_post( $inner_array['pble-newlabel'] ); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $translated_text; |
| 174 |
} |
| 175 |
add_filter( 'ngettext', 'wppb_le_text_strings', 8, 5 ); |
| 176 |
|
| 177 |
|
| 178 |
function wppb_le_remove_gettext_filter( $screen ) { |
| 179 |
if( is_object( $screen ) && $screen->id == 'profile-builder_page_pb-labels-edit' ) { |
| 180 |
remove_filter( 'gettext', 'wppb_le_text_strings', 8 ); |
| 181 |
} |
| 182 |
} |
| 183 |
add_action( 'current_screen', 'wppb_le_remove_gettext_filter' ); |
| 184 |
|
| 185 |
function wppb_le_remove_gettext_filter_from_ajax(){ |
| 186 |
remove_filter( 'gettext', 'wppb_le_text_strings', 8 ); |
| 187 |
} |
| 188 |
add_action('wp_ajax_wck_add_formpble', 'wppb_le_remove_gettext_filter_from_ajax'); |
| 189 |
add_action('wp_ajax_wck_refresh_listpble', 'wppb_le_remove_gettext_filter_from_ajax'); |
| 190 |
add_action('wp_ajax_wck_refresh_entrypble', 'wppb_le_remove_gettext_filter_from_ajax'); |
| 191 |
|
| 192 |
|
| 193 |
/* PB Labels Edit subpage content function */ |
| 194 |
function wppb_le_page() { |
| 195 |
// create Labels Edit page |
| 196 |
$args = array( |
| 197 |
'menu_title' => __( 'Labels Edit', 'profile-builder' ), |
| 198 |
'page_title' => __( 'Labels Edit', 'profile-builder' ), |
| 199 |
'menu_slug' => 'pb-labels-edit', |
| 200 |
'page_type' => 'submenu_page', |
| 201 |
'capability' => 'manage_options', |
| 202 |
'priority' => 5, |
| 203 |
'parent_slug' => 'profile-builder' |
| 204 |
); |
| 205 |
if( class_exists( 'WCK_Page_Creator_PB' ) ) { |
| 206 |
new WCK_Page_Creator_PB( $args ); |
| 207 |
} |
| 208 |
|
| 209 |
// array with Profile Builder strings to edit |
| 210 |
$wppb_strings = get_option( 'pble_backup', array() ); |
| 211 |
$pble_labels = $wppb_strings; |
| 212 |
|
| 213 |
|
| 214 |
// array with fields for Edit Labels metabox |
| 215 |
$pble_fields = array( |
| 216 |
array( 'type' => 'select', 'slug' => 'pble-label', 'title' => __( 'Label to Edit', 'profile-builder' ), 'default-option' => true, 'values' => $pble_labels, 'options' => $pble_labels, 'description' => __( 'Here you will see the default label so you can copy it.', 'profile-builder' ) ), |
| 217 |
array( 'type' => 'textarea', 'slug' => 'pble-newlabel', 'title' => __( 'New Label', 'profile-builder' ) ), |
| 218 |
); |
| 219 |
|
| 220 |
// create Edit Labels metabox |
| 221 |
$pble_args = array( |
| 222 |
'metabox_id' => 'pble-id', |
| 223 |
'metabox_title' => __( 'Edit Labels', 'profile-builder' ), |
| 224 |
'post_type' => 'pb-labels-edit', |
| 225 |
'meta_name' => 'pble', |
| 226 |
'meta_array' => $pble_fields, |
| 227 |
'context' => 'option' |
| 228 |
); |
| 229 |
if( class_exists( 'Wordpress_Creation_Kit_PB' ) ) { |
| 230 |
new Wordpress_Creation_Kit_PB( $pble_args ); |
| 231 |
} |
| 232 |
} |
| 233 |
add_action( 'init', 'wppb_le_page', 11 ); |
| 234 |
|
| 235 |
// add Rescan side meta-box |
| 236 |
function wppb_le_side_metabox() { |
| 237 |
add_meta_box( |
| 238 |
'pble-id-side', |
| 239 |
__( 'Rescan Lables', 'profile-builder' ), |
| 240 |
'wppb_le_rescan_button', |
| 241 |
'profile-builder_page_pb-labels-edit', |
| 242 |
'side' |
| 243 |
); |
| 244 |
} |
| 245 |
add_action( 'add_meta_boxes', 'wppb_le_side_metabox' ); |
| 246 |
|
| 247 |
// Rescan side meta-box content |
| 248 |
function wppb_le_rescan_button() { |
| 249 |
?> |
| 250 |
<div class="wrap"> |
| 251 |
<?php echo '<p>'. esc_html__( 'Rescan all Profile Builder labels.', 'profile-builder' ) .'</p>'; ?> |
| 252 |
|
| 253 |
<form action="" method="post"> |
| 254 |
<input type="hidden" name="wppb_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wppb_rescan_labels' ) ); ?>" /> |
| 255 |
<input type="submit" class="button-primary" name="rescan" value="Rescan" /> |
| 256 |
</form> |
| 257 |
</div> |
| 258 |
<?php |
| 259 |
} |
| 260 |
|
| 261 |
// add Informations side meta-box |
| 262 |
function wppb_le_info_side_metabox() { |
| 263 |
add_meta_box( |
| 264 |
'pble-id-side-info', |
| 265 |
__( 'Informations', 'profile-builder' ), |
| 266 |
'wppb_le_info', |
| 267 |
'profile-builder_page_pb-labels-edit', |
| 268 |
'side' |
| 269 |
); |
| 270 |
} |
| 271 |
add_action( 'add_meta_boxes', 'wppb_le_info_side_metabox' ); |
| 272 |
|
| 273 |
// Informations side meta-box content |
| 274 |
function wppb_le_info() { |
| 275 |
?> |
| 276 |
<div class="wrap"> |
| 277 |
<p><b> <?php echo esc_html__( 'Variables:', 'profile-builder' ) ?> </b></p> |
| 278 |
<ul> |
| 279 |
<li>%1$s</li> |
| 280 |
<li>%2$s</li> |
| 281 |
<li>%s</li> |
| 282 |
<li>etc.</li> |
| 283 |
</ul> |
| 284 |
<p><b> <?php echo esc_html__( 'Place them like in the default string!', 'profile-builder' ) ?> </b></p> |
| 285 |
<p><?php echo esc_html__( 'Example:', 'profile-builder' ) ?></p> |
| 286 |
<p> |
| 287 |
<b><?php echo esc_html__( 'Old Label', 'profile-builder' ) ?>:</b><br>in %1$d sec, click %2$s.%3$s<br> |
| 288 |
<b><?php echo esc_html__( 'New Label', 'profile-builder' ) ?>:</b><br>click %2$s.%3$s in %1$d sec<br> |
| 289 |
</p> |
| 290 |
<a href="http://www.cozmoslabs.com/?p=40126" target="_blank"><?php echo esc_html__( 'Read more detailed informations', 'profile-builder' ) ?></a> |
| 291 |
</div> |
| 292 |
<?php |
| 293 |
} |
| 294 |
|
| 295 |
// add Import and Export side meta-box |
| 296 |
function wppb_le_impexp_metabox() { |
| 297 |
add_meta_box( |
| 298 |
'pble-id-side-impexp', |
| 299 |
__( 'Import and Export Labels', 'profile-builder' ), |
| 300 |
'wppb_le_impexp_content', |
| 301 |
'profile-builder_page_pb-labels-edit', |
| 302 |
'side' |
| 303 |
); |
| 304 |
} |
| 305 |
add_action( 'add_meta_boxes', 'wppb_le_impexp_metabox' ); |
| 306 |
|
| 307 |
// Import and Export side meta-box content |
| 308 |
function wppb_le_impexp_content() { |
| 309 |
// call import function |
| 310 |
wppb_le_import(); |
| 311 |
?> |
| 312 |
<p> |
| 313 |
<?php esc_html_e( 'Import Labels from a .json file.', 'profile-builder' ); ?> |
| 314 |
<br> |
| 315 |
<?php esc_html_e( 'Easily import the labels from another site.', 'profile-builder' ); ?> |
| 316 |
</p> |
| 317 |
<form name="pble-upload" method="post" action="" enctype= "multipart/form-data"> |
| 318 |
|
| 319 |
<input type="hidden" name="wppb_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wppb_import_labels' ) ); ?>" /> |
| 320 |
|
| 321 |
<div class="wrap"> |
| 322 |
<input type="file" name="pble-upload" value="pble-upload" id="pble-upload" /> |
| 323 |
</div> |
| 324 |
<div class="wrap"> |
| 325 |
<input class="button-primary" type="submit" name="pble-import" value=<?php esc_html_e( 'Import', 'profile-builder' ); ?> id="pble-import" onclick="return confirm( '<?php esc_html_e( 'This will overwrite all your old edited labels!\nAre you sure you want to continue?', 'profile-builder' ); ?>' )" /> |
| 326 |
</div> |
| 327 |
</form> |
| 328 |
<hr> |
| 329 |
<p> |
| 330 |
<?php esc_html_e( 'Export Labels as a .json file.', 'profile-builder' ); ?> |
| 331 |
<br> |
| 332 |
<?php esc_html_e( 'Easily import the labels into another site.', 'profile-builder' ); ?> |
| 333 |
</p> |
| 334 |
<div class="wrap"> |
| 335 |
<form action="" method="post"> |
| 336 |
<input type="hidden" name="wppb_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wppb_export_labels' ) ); ?>" /> |
| 337 |
<input class="button-primary" type="submit" name="pble-export" value=<?php esc_html_e( 'Export', 'profile-builder' ); ?> id="pble-export" /> |
| 338 |
</form> |
| 339 |
</div> |
| 340 |
<?php |
| 341 |
} |
| 342 |
|
| 343 |
/* function that check for already edited labels */ |
| 344 |
function wppb_le_check_for_errors( $message, $fields, $required_fields, $meta_name, $posted_values, $post_id ) { |
| 345 |
if ( $meta_name == 'pble' ) { |
| 346 |
/* todo: broken check for added fields so you can't edit same label twice - fix it for future version |
| 347 |
$pble_posted_labels = get_option( $meta_name, 'not_set' ); |
| 348 |
$posted_labels = array(); |
| 349 |
|
| 350 |
|
| 351 |
if( ! empty( $pble_posted_labels ) ) { |
| 352 |
foreach( $pble_posted_labels as $label ) { |
| 353 |
$posted_labels[] = $label['pble-label']; |
| 354 |
} |
| 355 |
|
| 356 |
if( ( in_array( $posted_values['pble-label'], $posted_labels ) ) ) { |
| 357 |
$message = __( "This label is already edited!", 'profile-builder' ); |
| 358 |
} |
| 359 |
} |
| 360 |
*/ |
| 361 |
|
| 362 |
if( $posted_values['pble-label'] == '' ) { |
| 363 |
$message = __( "You must select a label to edit!", 'profile-builder' ); |
| 364 |
} |
| 365 |
} |
| 366 |
return $message; |
| 367 |
} |
| 368 |
add_filter( 'wck_extra_message', 'wppb_le_check_for_errors', 10, 6 ); |
| 369 |
|
| 370 |
/* function that change table header */ |
| 371 |
function wppb_le_header( $list_header ){ |
| 372 |
$delete_all_nonce = wp_create_nonce( 'pble-delete-all-entries' ); |
| 373 |
|
| 374 |
return '<thead><tr><th class="wck-number">#</th><th class="wck-content">'. __( 'Labels', 'profile-builder' ) .'</th><th class="wck-edit">'. __( 'Edit', 'profile-builder' ) .'</th><th class="wck-delete"><a id="wppb-delete-all-fields" class="wppb-delete-all-fields" onclick="wppb_le_delete_all_fields(event, this.id, \'' . esc_js($delete_all_nonce) . '\')" title="' . __('Delete all', 'profile-builder') . '" href="#">'. __( 'Delete all', 'profile-builder' ) .'</a></th></tr></thead>'; |
| 375 |
} |
| 376 |
add_action( 'wck_metabox_content_header_pble', 'wppb_le_header' ); |
| 377 |
|
| 378 |
/* function that delete all edited labels */ |
| 379 |
add_action("wp_ajax_pble_delete_all_fields", 'wppb_le_delete_all_fields_callback' ); |
| 380 |
function wppb_le_delete_all_fields_callback(){ |
| 381 |
check_ajax_referer( "pble-delete-all-entries" ); |
| 382 |
|
| 383 |
if( ! empty( $_POST['meta'] ) ) |
| 384 |
$meta_name = sanitize_text_field( $_POST['meta'] ); |
| 385 |
else |
| 386 |
$meta_name = ''; |
| 387 |
|
| 388 |
if( $meta_name == 'pble' ) { |
| 389 |
delete_option( 'pble' ); |
| 390 |
} |
| 391 |
exit; |
| 392 |
} |
| 393 |
|
| 394 |
/* function that calls chosen after refresh */ |
| 395 |
function wppb_le_chosen_pble( $id ) { |
| 396 |
echo "<script type=\"text/javascript\">wppb_le_chosen(); wppb_le_description( jQuery( '.update_container_pble .mb-select' ) ); </script>"; |
| 397 |
} |
| 398 |
add_action( "wck_ajax_add_form_pble", "wppb_le_chosen_pble" ); |
| 399 |
add_action( "wck_after_adding_form_pble", "wppb_le_chosen_pble" ); |
| 400 |
|
| 401 |
/* import class arguments and call */ |
| 402 |
function wppb_le_import() { |
| 403 |
if( isset( $_POST['pble-import'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_import_labels' ) ) { |
| 404 |
if( isset( $_FILES['pble-upload'] ) ) { |
| 405 |
$pble_args = array( |
| 406 |
'pble' |
| 407 |
); |
| 408 |
|
| 409 |
$pble_json_upload = new WPPB_LE_Import( $pble_args ); |
| 410 |
$pble_json_upload->upload_json_file(); |
| 411 |
/* show error/success messages */ |
| 412 |
$pble_messages = $pble_json_upload->get_messages(); |
| 413 |
foreach ( $pble_messages as $pble_message ) { |
| 414 |
echo '<div id="message" class='. esc_attr( $pble_message['type'] ) .'><p>'. $pble_message['message'] .'</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/* export class arguments and call */ |
| 421 |
add_action( 'admin_init', 'wppb_le_export' ); |
| 422 |
function wppb_le_export() { |
| 423 |
if( isset( $_POST['pble-export'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_export_labels' ) ) { |
| 424 |
$check_export = get_option( 'pble', 'not_set' ); |
| 425 |
if( empty( $check_export ) || $check_export === 'not_set' ) { |
| 426 |
echo '<div id="message" class="error"><p>' . esc_html__('No labels edited, nothing to export!', 'profile-builder') . '</p></div>'; |
| 427 |
} else { |
| 428 |
$pble_args = array( |
| 429 |
'pble' |
| 430 |
); |
| 431 |
|
| 432 |
$pble_prefix = 'PBLE_'; |
| 433 |
$pble_json_export = new WPPB_LE_Export( $pble_args ); |
| 434 |
$pble_json_export->download_to_json_format( $pble_prefix ); |
| 435 |
} |
| 436 |
} |
| 437 |
} |