Changeset 2807873
- Timestamp:
- 10/31/2022 11:40:06 AM (3 years ago)
- File:
-
- 1 edited
-
hide-edit-with-elementor/trunk/includes/plugin.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
hide-edit-with-elementor/trunk/includes/plugin.php
r2804545 r2807873 1 1 <?php 2 2 3 4 3 5 // Add meta box to the side bar on edit page/post screens 6 4 7 add_action( 'add_meta_boxes', 'hewe_add_meta_box' ); 5 8 9 10 6 11 // Add the box 12 7 13 function hewe_add_meta_box(){ 14 8 15 // Only show if they can edit WordPress options eg. administrator 16 9 17 if( current_user_can( 'manage_options', $post_id ) ) { 18 10 19 // Add meta box on all type of post to hide Elementor - specifying a post type where the '' is below would limit it 20 11 21 add_meta_box( 'hewe_settings', 'Hide Edit With Elementor', 'hewe_meta_box_callback', '', 'side' ); 22 12 23 } 24 13 25 } 14 26 27 28 15 29 // HTML code of the meta box 30 16 31 function hewe_meta_box_callback( $post, $meta ){ 32 17 33 // Use nonce for verification 34 18 35 wp_nonce_field( plugin_basename(__FILE__), 'hewe_noncename' ); 36 19 37 // field value 38 20 39 $value = get_post_meta( $post->ID, 'hewe_switch', 1 ); ?> 40 21 41 <p><input type="checkbox" id="hewe_switch" name="hewe_switch" value="true" <?php if ($value == 'true') { ?> checked<?php } ?>> <label for="hewe_switch">Hide</label></p> 42 22 43 <?php 44 23 45 } 24 46 47 48 25 49 // Save data when the post is saved 50 26 51 add_action( 'save_post', 'hewe_save_postdata' ); 27 52 53 54 28 55 function hewe_save_postdata( $post_id ) { 56 29 57 // check the nonce of our page, because save_post can be called from another location. 58 30 59 if ( ! wp_verify_nonce( $_POST['hewe_noncename'], plugin_basename(__FILE__) ) ) 60 31 61 return; 62 32 63 // if this is autosave do nothing 64 33 65 if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 66 34 67 return; 68 35 69 // check user permission 70 36 71 if( ! current_user_can( 'manage_options', $post_id ) ) 72 37 73 return; 74 38 75 // clear the value of the input field. 76 39 77 $hewe_data = sanitize_text_field( $_POST['hewe_switch'] ); 78 40 79 // Update data in the database. 80 41 81 update_post_meta( $post_id, 'hewe_switch', $hewe_data ); 82 42 83 } 84 85 43 86 44 87 // Load JS and give it access to the PHP variable 45 88 89 90 46 91 function hewe_script() { 92 47 93 if (is_user_logged_in()) { 94 48 95 // Compile a list of all posts / pages etc that have 'Hide Edit With Elementor' switched on and place in an array 96 49 97 global $wpdb; 50 $hewe_query_raw = $wpdb->get_results("SELECT post_id FROM wp_postmeta WHERE meta_key = 'hewe_switch' AND meta_value = 'true'"); 98 99 $hewe_query_raw = $wpdb->get_results("SELECT post_id FROM ".$wpdb->prefix."postmeta WHERE meta_key = 'hewe_switch' AND meta_value = 'true'"); 100 51 101 foreach($hewe_query_raw as $hewe_post): $hewe_query[] = $hewe_post->post_id; endforeach; 102 52 103 // Enqueue scripts 104 53 105 wp_enqueue_script( 'hewe_script', HEWE_PLUGINURL.'assets/js/script.js', array(), date('YmdHi', filemtime(plugin_dir_path( __FILE__ ).'../assets/js/script.js')), true); 106 54 107 // Expose the array to the script so it can be used in the jQuery 108 55 109 $hewevar = array( 110 56 111 'pagelist' => $hewe_query 112 57 113 ); 114 58 115 wp_localize_script( 'hewe_script', 'hewevar', $hewevar ); 116 59 117 } 118 60 119 } 61 120 121 122 62 123 add_action( 'admin_enqueue_scripts', 'hewe_script', 2000 ); 124 63 125 add_action( 'wp_enqueue_scripts', 'hewe_script', 2000 );
Note: See TracChangeset
for help on using the changeset viewer.