Plugin Directory

Changeset 2807873


Ignore:
Timestamp:
10/31/2022 11:40:06 AM (3 years ago)
Author:
StuckOn_dev
Message:

v1.1 Fix to allow for different database prefixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • hide-edit-with-elementor/trunk/includes/plugin.php

    r2804545 r2807873  
    11<?php
    22
     3
     4
    35// Add meta box to the side bar on edit page/post screens
     6
    47add_action( 'add_meta_boxes', 'hewe_add_meta_box' );
    58
     9
     10
    611// Add the box
     12
    713function hewe_add_meta_box(){
     14
    815    // Only show if they can edit WordPress options eg. administrator
     16
    917    if( current_user_can( 'manage_options', $post_id ) ) {
     18
    1019        // Add meta box on all type of post to hide Elementor - specifying a post type where the '' is below would limit it
     20
    1121        add_meta_box( 'hewe_settings', 'Hide Edit With Elementor', 'hewe_meta_box_callback', '', 'side' );
     22
    1223    }
     24
    1325}
    1426
     27
     28
    1529// HTML code of the meta box
     30
    1631function hewe_meta_box_callback( $post, $meta ){   
     32
    1733    // Use nonce for verification
     34
    1835    wp_nonce_field( plugin_basename(__FILE__), 'hewe_noncename' );
     36
    1937    // field value
     38
    2039    $value = get_post_meta( $post->ID, 'hewe_switch', 1 ); ?>
     40
    2141    <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
    2243    <?php
     44
    2345}
    2446
     47
     48
    2549// Save data when the post is saved
     50
    2651add_action( 'save_post', 'hewe_save_postdata' );
    2752
     53
     54
    2855function hewe_save_postdata( $post_id ) {
     56
    2957    // check the nonce of our page, because save_post can be called from another location.
     58
    3059    if ( ! wp_verify_nonce( $_POST['hewe_noncename'], plugin_basename(__FILE__) ) )
     60
    3161        return;
     62
    3263    // if this is autosave do nothing
     64
    3365    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
     66
    3467        return;
     68
    3569    // check user permission
     70
    3671    if( ! current_user_can( 'manage_options', $post_id ) )
     72
    3773        return;
     74
    3875    // clear the value of the input field.
     76
    3977    $hewe_data = sanitize_text_field( $_POST['hewe_switch'] );
     78
    4079    // Update data in the database.
     80
    4181    update_post_meta( $post_id, 'hewe_switch', $hewe_data );
     82
    4283}
     84
     85
    4386
    4487// Load JS and give it access to the PHP variable
    4588
     89
     90
    4691function hewe_script() {
     92
    4793    if (is_user_logged_in()) {
     94
    4895        // Compile a list of all posts / pages etc that have 'Hide Edit With Elementor' switched on and place in an array
     96
    4997        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
    51101        foreach($hewe_query_raw as $hewe_post): $hewe_query[] = $hewe_post->post_id; endforeach;
     102
    52103        // Enqueue scripts
     104
    53105        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
    54107        // Expose the array to the script so it can be used in the jQuery
     108
    55109        $hewevar = array(
     110
    56111            'pagelist' => $hewe_query
     112
    57113        );
     114
    58115        wp_localize_script( 'hewe_script', 'hewevar', $hewevar );
     116
    59117    }
     118
    60119}
    61120
     121
     122
    62123add_action( 'admin_enqueue_scripts', 'hewe_script', 2000 );
     124
    63125add_action( 'wp_enqueue_scripts', 'hewe_script', 2000 );
Note: See TracChangeset for help on using the changeset viewer.