Plugin Directory

Changeset 3213937


Ignore:
Timestamp:
12/27/2024 07:32:28 PM (12 months ago)
Author:
martin7ba
Message:

Version 2.0

Location:
whp-hide-posts
Files:
32 added
7 edited

Legend:

Unmodified
Added
Removed
  • whp-hide-posts/trunk/README.md

    r3206303 r3213937  
    66Tested up to: 6.7.1
    77Requires PHP: 7.3
    8 Stable tag: 1.1.1
     8Stable tag: 2.0
    99License: GPLv3 or later
    1010License URI: http://www.gnu.org/licenses/gpl-3.0.html
    11 
    12 Allows you to hide any posts on the home page, category page, search page, tags page, authors page, RSS Feed, REST API, Post Navigation and more.
    1311
    1412== Description ==
     
    6563
    6664== Changelog ==
     65
     66= 2.0 =
     67_Release Date - 27 December 2024_
     68
     69- Added custom database table to hold the hide post flags.
     70- All new Hide flags on posts will be saved in the new database table. A fallback to old data is also added in case the old data is not yet migrated to the new table.
     71- Added option to migrate the old data from wp_postmeta table to the new table.
    6772
    6873= 1.1.1 =
  • whp-hide-posts/trunk/inc/admin/class-dashboard.php

    r2674320 r3213937  
    2727        add_action( 'admin_init', array( $this, 'register_settings' ) );
    2828        add_action( 'admin_menu', array( $this, 'menu' ) );
     29        add_action( 'admin_notices', array( $this, 'migrate_data_notice' ) );
     30        add_action( 'admin_init', array( $this, 'handle_migration_action' ) );
    2931    }
    3032
     
    6769        register_setting( 'whp-settings-group', 'whp_disable_hidden_on_column' );
    6870    }
     71
     72    /**
     73     * Migrate hide posts data from meta to table
     74     *
     75     * @return void
     76     */
     77    public function migrate_meta_to_table() {
     78        $data_migrated = get_option( 'whp_data_migrated', false );
     79
     80        if ( $data_migrated ) {
     81            return;
     82        }
     83
     84        global $wpdb;
     85
     86        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     87
     88        $table_exists = $wpdb->get_var(
     89            $wpdb->prepare(
     90                "SHOW TABLES LIKE %s",
     91                $table_name
     92            )
     93        );
     94
     95        if ( $table_exists !== $table_name ) {
     96            return;
     97        }
     98
     99        $meta_keys = [
     100            '_whp_hide_on_frontpage'    => 'hide_on_frontpage',
     101            '_whp_hide_on_blog_page'    => 'hide_on_blog_page',
     102            '_whp_hide_on_cpt_archive'  => 'hide_on_cpt_archive',
     103            '_whp_hide_on_categories'   => 'hide_on_categories',
     104            '_whp_hide_on_search'       => 'hide_on_search',
     105            '_whp_hide_on_tags'         => 'hide_on_tags',
     106            '_whp_hide_on_authors'      => 'hide_on_authors',
     107            '_whp_hide_on_date'         => 'hide_on_date',
     108            '_whp_hide_in_rss_feed'     => 'hide_in_rss_feed',
     109            '_whp_hide_on_store'        => 'hide_on_store',
     110            '_whp_hide_on_product_category' => 'hide_on_product_category',
     111            '_whp_hide_on_single_post_page' => 'hide_on_single_post_page',
     112        ];
     113
     114        foreach ( $meta_keys as $meta_key => $condition ) {
     115            $posts = $wpdb->get_results(
     116                $wpdb->prepare(
     117                    "
     118                    SELECT post_id
     119                    FROM {$wpdb->postmeta}
     120                    WHERE meta_key = %s
     121                    ",
     122                    $meta_key
     123                )
     124            );
     125
     126            foreach ( $posts as $post ) {
     127                $exist = whp_plugin()->get_whp_meta( $post->post_id, $condition );
     128
     129                if ( $exist ) {
     130                    continue;
     131                }
     132
     133                $wpdb->insert(
     134                    $table_name,
     135                    array(
     136                        'post_id'   => $post->post_id,
     137                        'condition' => $condition,
     138                    ),
     139                    array(
     140                        '%d',
     141                        '%s',
     142                    )
     143                );
     144
     145                delete_post_meta( $post->post_id, $meta_key );
     146            }
     147        }
     148
     149        update_option( 'whp_data_migrated', true );
     150    }
     151
     152    /**
     153     * Notice to migrate data
     154     *
     155     * @return void
     156     */
     157    public function migrate_data_notice() {
     158        if ( ! current_user_can( 'manage_options' ) ) {
     159            return;
     160        }
     161
     162        $data_migrated_notice_closed = get_option( 'whp_data_migrated_notice_closed', false );
     163
     164        if ( $data_migrated_notice_closed ) {
     165            return;
     166        }
     167
     168        $data_migrated = get_option( 'whp_data_migrated', false );
     169
     170        if ( $data_migrated ) {
     171            $action_url = add_query_arg(
     172                array(
     173                    'action' => 'whp_hide_posts_migration_complete_notice_close',
     174                    '__nonce' => wp_create_nonce( 'whp-hide-posts-migration-complete-nonce' ),
     175                ),
     176                admin_url()
     177            );
     178
     179            echo '<div class="notice notice-success">';
     180            echo '<p>Migaration Complete.</p>';
     181            echo '<p><a href="' . esc_url( $action_url ) . '" class="button button-primary">Close Notice</a></p>';
     182            echo '</div>';
     183            return;
     184        }
     185
     186        $action_url = add_query_arg(
     187            array(
     188                'action' => 'whp_hide_posts_migrate_data',
     189                '__nonce' => wp_create_nonce( 'whp-hide-posts-migrate-data-nonce' ),
     190            ),
     191            admin_url()
     192        );
     193
     194        echo '<div class="notice notice-warning is-dismissible">';
     195        echo '<p>Important: We implemented new table for managing the hide flags in our plugin which optimizes the query and improve overall performance. <strong>Please create database backup before proceeding, just in case.</strong></p>';
     196        echo '<p><a href="' . esc_url( $action_url ) . '" class="button button-primary">Migrate Hide Post Data</a></p>';
     197        echo '</div>';
     198    }
     199
     200    /**
     201     * Handle the migration action
     202     *
     203     * @return void
     204     */
     205    public function handle_migration_action() {
     206        $data_migrated = get_option( 'whp_data_migrated', false );
     207
     208        if ( $data_migrated ) {
     209            $data_migrated_notice_closed = get_option( 'whp_data_migrated_notice_closed', false );
     210
     211            if ( ! $data_migrated_notice_closed ) {
     212                if ( ! isset( $_GET['action'] ) || 'whp_hide_posts_migration_complete_notice_close' !== $_GET['action'] ) {
     213                    return;
     214                }
     215
     216                if ( ! isset( $_GET['__nonce'] ) || ! wp_verify_nonce( $_GET['__nonce'], 'whp-hide-posts-migration-complete-nonce' ) ) {
     217                    return;
     218                }
     219
     220                update_option( 'whp_data_migrated_notice_closed', true );
     221
     222                wp_safe_redirect( remove_query_arg( array( 'action', '__nonce' ) ) );
     223                exit;
     224            }
     225
     226            return;
     227        }
     228
     229        if ( ! isset( $_GET['action'] ) || 'whp_hide_posts_migrate_data' !== $_GET['action'] ) {
     230            return;
     231        }
     232
     233        if ( ! isset( $_GET['__nonce'] ) || ! wp_verify_nonce( $_GET['__nonce'], 'whp-hide-posts-migrate-data-nonce' ) ) {
     234            return;
     235        }
     236
     237        $this->migrate_meta_to_table();
     238
     239        wp_safe_redirect( remove_query_arg( array( 'action', '__nonce' ) ) );
     240        exit;
     241    }
    69242}
  • whp-hide-posts/trunk/inc/admin/class-post-hide-metabox.php

    r2794480 r3213937  
    129129        }
    130130
    131         $whp_hide_on_frontpage        = get_post_meta( $post_id, '_whp_hide_on_frontpage', true );
    132         $whp_hide_on_categories       = get_post_meta( $post_id, '_whp_hide_on_categories', true );
    133         $whp_hide_on_search           = get_post_meta( $post_id, '_whp_hide_on_search', true );
    134         $whp_hide_on_tags             = get_post_meta( $post_id, '_whp_hide_on_tags', true );
    135         $whp_hide_on_authors          = get_post_meta( $post_id, '_whp_hide_on_authors', true );
    136         $whp_hide_in_rss_feed         = get_post_meta( $post_id, '_whp_hide_in_rss_feed', true );
    137         $whp_hide_on_blog_page        = get_post_meta( $post_id, '_whp_hide_on_blog_page', true );
    138         $whp_hide_on_date             = get_post_meta( $post_id, '_whp_hide_on_date', true );
    139         $whp_hide_on_post_navigation  = get_post_meta( $post_id, '_whp_hide_on_post_navigation', true );
    140         $whp_hide_on_recent_posts     = get_post_meta( $post_id, '_whp_hide_on_recent_posts', true );
    141         $whp_hide_on_cpt_archive      = get_post_meta( $post_id, '_whp_hide_on_cpt_archive', true );
    142         $whp_hide_on_archive          = get_post_meta( $post_id, '_whp_hide_on_archive', true );
    143         $whp_hide_on_rest_api         = get_post_meta( $post_id, '_whp_hide_on_rest_api', true );
    144         $whp_hide_on_single_post_page = get_post_meta( $post_id, '_whp_hide_on_single_post_page', true );
     131        $data_migrated = get_option( 'whp_data_migrated', false );
     132
     133        $fallaback = ! $data_migrated;
     134
     135        $whp_hide_on_frontpage        = whp_plugin()->get_whp_meta( $post_id, 'hide_on_frontpage', $fallaback  );
     136        $whp_hide_on_categories       = whp_plugin()->get_whp_meta( $post_id, 'hide_on_categories', $fallaback  );
     137        $whp_hide_on_search           = whp_plugin()->get_whp_meta( $post_id, 'hide_on_search', $fallaback  );
     138        $whp_hide_on_tags             = whp_plugin()->get_whp_meta( $post_id, 'hide_on_tags', $fallaback  );
     139        $whp_hide_on_authors          = whp_plugin()->get_whp_meta( $post_id, 'hide_on_authors', $fallaback  );
     140        $whp_hide_in_rss_feed         = whp_plugin()->get_whp_meta( $post_id, 'hide_in_rss_feed', $fallaback  );
     141        $whp_hide_on_blog_page        = whp_plugin()->get_whp_meta( $post_id, 'hide_on_blog_page', $fallaback  );
     142        $whp_hide_on_date             = whp_plugin()->get_whp_meta( $post_id, 'hide_on_date', $fallaback  );
     143        $whp_hide_on_post_navigation  = whp_plugin()->get_whp_meta( $post_id, 'hide_on_post_navigation', $fallaback  );
     144        $whp_hide_on_recent_posts     = whp_plugin()->get_whp_meta( $post_id, 'hide_on_recent_posts', $fallaback  );
     145        $whp_hide_on_cpt_archive      = whp_plugin()->get_whp_meta( $post_id, 'hide_on_cpt_archive', $fallaback  );
     146        $whp_hide_on_archive          = whp_plugin()->get_whp_meta( $post_id, 'hide_on_archive', $fallaback  );
     147        $whp_hide_on_rest_api         = whp_plugin()->get_whp_meta( $post_id, 'hide_on_rest_api', $fallaback  );
     148        $whp_hide_on_single_post_page = whp_plugin()->get_whp_meta( $post_id, 'hide_on_single_post_page', $fallaback );
    145149
    146150        if ( whp_plugin()->is_woocommerce_active() && whp_plugin()->is_woocommerce_product() ) {
    147             $whp_hide_on_store            = get_post_meta( $post_id, '_whp_hide_on_store', true );
    148             $whp_hide_on_product_category = get_post_meta( $post_id, '_whp_hide_on_product_category', true );
     151            $whp_hide_on_store            = whp_plugin()->get_whp_meta( $post_id, 'hide_on_store', $fallaback );
     152            $whp_hide_on_product_category = whp_plugin()->get_whp_meta( $post_id, 'hide_on_product_category', $fallaback );
    149153        }
    150154
     
    232236        wp_nonce_field( 'wp_metabox_nonce', 'wp_metabox_nonce_value' );
    233237
    234         $whp_hide_on_frontpage        = get_post_meta( $post->ID, '_whp_hide_on_frontpage', true );
    235         $whp_hide_on_categories       = get_post_meta( $post->ID, '_whp_hide_on_categories', true );
    236         $whp_hide_on_search           = get_post_meta( $post->ID, '_whp_hide_on_search', true );
    237         $whp_hide_on_tags             = get_post_meta( $post->ID, '_whp_hide_on_tags', true );
    238         $whp_hide_on_authors          = get_post_meta( $post->ID, '_whp_hide_on_authors', true );
    239         $whp_hide_in_rss_feed         = get_post_meta( $post->ID, '_whp_hide_in_rss_feed', true );
    240         $whp_hide_on_blog_page        = get_post_meta( $post->ID, '_whp_hide_on_blog_page', true );
    241         $whp_hide_on_date             = get_post_meta( $post->ID, '_whp_hide_on_date', true );
    242         $whp_hide_on_post_navigation  = get_post_meta( $post->ID, '_whp_hide_on_post_navigation', true );
    243         $whp_hide_on_recent_posts     = get_post_meta( $post->ID, '_whp_hide_on_recent_posts', true );
    244         $whp_hide_on_cpt_archive      = get_post_meta( $post->ID, '_whp_hide_on_cpt_archive', true );
    245         $whp_hide_on_archive          = get_post_meta( $post->ID, '_whp_hide_on_archive', true );
    246         $whp_hide_on_rest_api         = get_post_meta( $post->ID, '_whp_hide_on_rest_api', true );
    247         $whp_hide_on_single_post_page = get_post_meta( $post->ID, '_whp_hide_on_single_post_page', true );
     238        $post_id = $post->ID;
     239
     240        $data_migrated = get_option( 'whp_data_migrated', false );
     241
     242        $fallaback = ! $data_migrated;
     243
     244        $whp_hide_on_frontpage        = whp_plugin()->get_whp_meta( $post_id, 'hide_on_frontpage', $fallaback  );
     245        $whp_hide_on_categories       = whp_plugin()->get_whp_meta( $post_id, 'hide_on_categories', $fallaback  );
     246        $whp_hide_on_search           = whp_plugin()->get_whp_meta( $post_id, 'hide_on_search', $fallaback  );
     247        $whp_hide_on_tags             = whp_plugin()->get_whp_meta( $post_id, 'hide_on_tags', $fallaback  );
     248        $whp_hide_on_authors          = whp_plugin()->get_whp_meta( $post_id, 'hide_on_authors', $fallaback  );
     249        $whp_hide_in_rss_feed         = whp_plugin()->get_whp_meta( $post_id, 'hide_in_rss_feed', $fallaback  );
     250        $whp_hide_on_blog_page        = whp_plugin()->get_whp_meta( $post_id, 'hide_on_blog_page', $fallaback  );
     251        $whp_hide_on_date             = whp_plugin()->get_whp_meta( $post_id, 'hide_on_date', $fallaback  );
     252        $whp_hide_on_post_navigation  = whp_plugin()->get_whp_meta( $post_id, 'hide_on_post_navigation', $fallaback  );
     253        $whp_hide_on_recent_posts     = whp_plugin()->get_whp_meta( $post_id, 'hide_on_recent_posts', $fallaback  );
     254        $whp_hide_on_cpt_archive      = whp_plugin()->get_whp_meta( $post_id, 'hide_on_cpt_archive', $fallaback  );
     255        $whp_hide_on_archive          = whp_plugin()->get_whp_meta( $post_id, 'hide_on_archive', $fallaback  );
     256        $whp_hide_on_rest_api         = whp_plugin()->get_whp_meta( $post_id, 'hide_on_rest_api', $fallaback  );
     257        $whp_hide_on_single_post_page = whp_plugin()->get_whp_meta( $post_id, 'hide_on_single_post_page', $fallaback );
    248258
    249259        if ( whp_plugin()->is_woocommerce_active() && whp_plugin()->is_woocommerce_product() ) {
    250             $whp_hide_on_store            = get_post_meta( $post->ID, '_whp_hide_on_store', true );
    251             $whp_hide_on_product_category = get_post_meta( $post->ID, '_whp_hide_on_product_category', true );
     260            $whp_hide_on_store            = whp_plugin()->get_whp_meta( $post_id, 'hide_on_store', $fallaback );
     261            $whp_hide_on_product_category = whp_plugin()->get_whp_meta( $post_id, 'hide_on_product_category', $fallaback );
    252262        }
    253263
     
    295305
    296306        // Data to be stored in the database.
    297         $data['_whp_hide_on_frontpage']        = ! empty( $args['whp_hide_on_frontpage'] ) ? true : false;
    298         $data['_whp_hide_on_categories']       = ! empty( $args['whp_hide_on_categories'] ) ? true : false;
    299         $data['_whp_hide_on_search']           = ! empty( $args['whp_hide_on_search'] ) ? true : false;
    300         $data['_whp_hide_on_tags']             = ! empty( $args['whp_hide_on_tags'] ) ? true : false;
    301         $data['_whp_hide_on_authors']          = ! empty( $args['whp_hide_on_authors'] ) ? true : false;
    302         $data['_whp_hide_in_rss_feed']         = ! empty( $args['whp_hide_in_rss_feed'] ) ? true : false;
    303         $data['_whp_hide_on_blog_page']        = ! empty( $args['whp_hide_on_blog_page'] ) ? true : false;
    304         $data['_whp_hide_on_date']             = ! empty( $args['whp_hide_on_date'] ) ? true : false;
    305         $data['_whp_hide_on_post_navigation']  = ! empty( $args['whp_hide_on_post_navigation'] ) ? true : false;
    306         $data['_whp_hide_on_recent_posts']     = ! empty( $args['whp_hide_on_recent_posts'] ) ? true : false;
    307         $data['_whp_hide_on_archive']          = ! empty( $args['whp_hide_on_archive'] ) ? true : false;
    308         $data['_whp_hide_on_cpt_archive']      = ! empty( $args['whp_hide_on_cpt_archive'] ) ? true : false;
    309         $data['_whp_hide_on_rest_api']         = ! empty( $args['whp_hide_on_rest_api'] ) ? true : false;
    310         $data['_whp_hide_on_single_post_page'] = ! empty( $args['whp_hide_on_single_post_page'] ) ? true : false;
     307        $data['hide_on_frontpage']        = ! empty( $args['whp_hide_on_frontpage'] ) ? true : false;
     308        $data['hide_on_categories']       = ! empty( $args['whp_hide_on_categories'] ) ? true : false;
     309        $data['hide_on_search']           = ! empty( $args['whp_hide_on_search'] ) ? true : false;
     310        $data['hide_on_tags']             = ! empty( $args['whp_hide_on_tags'] ) ? true : false;
     311        $data['hide_on_authors']          = ! empty( $args['whp_hide_on_authors'] ) ? true : false;
     312        $data['hide_in_rss_feed']         = ! empty( $args['whp_hide_in_rss_feed'] ) ? true : false;
     313        $data['hide_on_blog_page']        = ! empty( $args['whp_hide_on_blog_page'] ) ? true : false;
     314        $data['hide_on_date']             = ! empty( $args['whp_hide_on_date'] ) ? true : false;
     315        $data['hide_on_post_navigation']  = ! empty( $args['whp_hide_on_post_navigation'] ) ? true : false;
     316        $data['hide_on_recent_posts']     = ! empty( $args['whp_hide_on_recent_posts'] ) ? true : false;
     317        $data['hide_on_archive']          = ! empty( $args['whp_hide_on_archive'] ) ? true : false;
     318        $data['hide_on_cpt_archive']      = ! empty( $args['whp_hide_on_cpt_archive'] ) ? true : false;
     319        $data['hide_on_rest_api']         = ! empty( $args['whp_hide_on_rest_api'] ) ? true : false;
     320        $data['hide_on_single_post_page'] = ! empty( $args['whp_hide_on_single_post_page'] ) ? true : false;
    311321
    312322        if ( whp_plugin()->is_woocommerce_active() && whp_plugin()->is_woocommerce_product() ) {
    313             $data['_whp_hide_on_store']            = ! empty( $args['whp_hide_on_store'] ) ? true : false;
    314             $data['_whp_hide_on_product_category'] = ! empty( $args['whp_hide_on_product_category'] ) ? true : false;
     323            $data['hide_on_store']            = ! empty( $args['whp_hide_on_store'] ) ? true : false;
     324            $data['hide_on_product_category'] = ! empty( $args['whp_hide_on_product_category'] ) ? true : false;
    315325        }
    316326
     
    341351    private function save_meta_data( $meta_data, $post_id ) {
    342352        foreach ( $meta_data as $key => $value ) {
    343             if ( get_post_meta( $post_id, $key, false ) ) {
    344                 update_post_meta( $post_id, $key, $value );
    345             } else {
    346                 add_post_meta( $post_id, $key, $value );
     353            $exist = whp_plugin()->get_whp_meta( $post_id, $key,$fallabacke );
     354            if ( $exist && ! $value ) {
     355                whp_plugin()->delete_whp_meta( $post_id, $key, true );
     356            } elseif ( ! $exist && $value ) {
     357                whp_plugin()->add_whp_meta( $post_id, $key );
    347358            }
    348359
    349             if ( ! $value ) {
    350                 delete_post_meta( $post_id, $key );
    351             }
     360            delete_post_meta( $post_id, '_whp_' . $key );
    352361        }
    353362    }
  • whp-hide-posts/trunk/inc/class-post-hide.php

    r2779263 r3213937  
    9696     */
    9797    public function exclude_posts( $query ) {
     98        global $wpdb;
     99
    98100        $q_post_type = $query->get( 'post_type' );
    99101
     
    105107            )
    106108        ) {
     109            $table_name = $wpdb->prefix . 'whp_posts_visibility';
     110            // Handle single post pages
    107111            if ( is_singular( $q_post_type ) && ! $query->is_main_query() ) {
    108                 $query->set( 'meta_key', '_whp_hide_on_single_post_page' );
     112                $hidden_posts = $wpdb->get_col(
     113                    $wpdb->prepare(
     114                        "SELECT DISTINCT post_id FROM {$table_name} WHERE `condition` = %s",
     115                        'hide_on_single_post_page'
     116                    )
     117                );
     118
     119                if ( ! empty( $hidden_posts ) ) {
     120                    $query->set( 'post__not_in', $hidden_posts );
     121                } else {
     122                    // Fallback to meta
     123                    $query->set( 'meta_key', '_whp_hide_on_single_post_page' );
     124                    $query->set( 'meta_compare', 'NOT EXISTS' );
     125                }
     126            } elseif ( ( is_front_page() && is_home() ) || is_front_page() ) {
     127                $this->exclude_by_condition( $query, 'hide_on_frontpage', '_whp_hide_on_frontpage' );
     128            } elseif ( is_home() ) {
     129                $this->exclude_by_condition( $query, 'hide_on_blog_page', '_whp_hide_on_blog_page' );
     130            } elseif ( is_post_type_archive( $q_post_type ) ) {
     131                $this->exclude_by_condition( $query, 'hide_on_cpt_archive', '_whp_hide_on_cpt_archive' );
     132            } elseif ( is_category( $q_post_type ) ) {
     133                $this->exclude_by_condition( $query, 'hide_on_categories', '_whp_hide_on_categories' );
     134            } elseif ( is_tag() ) {
     135                $this->exclude_by_condition( $query, 'hide_on_tags', '_whp_hide_on_tags' );
     136            } elseif ( is_author() ) {
     137                $this->exclude_by_condition( $query, 'hide_on_authors', '_whp_hide_on_authors' );
     138            } elseif ( is_date() ) {
     139                $this->exclude_by_condition( $query, 'hide_on_date', '_whp_hide_on_date' );
     140            } elseif ( is_search() ) {
     141                $this->exclude_by_condition( $query, 'hide_on_search', '_whp_hide_on_search' );
     142            } elseif ( is_feed() ) {
     143                $this->exclude_by_condition( $query, 'hide_in_rss_feed', '_whp_hide_in_rss_feed' );
     144            } elseif ( whp_plugin()->is_woocommerce_active() ) {
     145                if ( is_shop() ) {
     146                    $this->exclude_by_condition( $query, 'hide_on_store', '_whp_hide_on_store' );
     147                } elseif ( is_product_category() ) {
     148                    $this->exclude_by_condition( $query, 'hide_on_product_category', '_whp_hide_on_product_category' );
     149                }
     150            } elseif ( is_archive() ) {
     151                $this->exclude_by_condition( $query, 'hide_on_archive', '_whp_hide_on_archive' );
     152            }
     153        }
     154    }
     155
     156    /**
     157     * Exclude posts by a condition using the custom table, with a fallback to post meta.
     158     *
     159     * @param WP_Query $query The query object.
     160     * @param string   $condition The condition to check in the custom table.
     161     * @param string   $meta_key The meta key to check for fallback.
     162     */
     163    private function exclude_by_condition( &$query, $condition, $meta_key ) {
     164        global $wpdb;
     165
     166        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     167
     168        $hidden_posts = $wpdb->get_col(
     169            $wpdb->prepare(
     170                "SELECT DISTINCT post_id FROM {$table_name} WHERE `condition` = %s",
     171                $condition
     172            )
     173        );
     174
     175        if ( ! empty( $hidden_posts ) ) {
     176            $query->set( 'post__not_in', $hidden_posts );
     177        } else {
     178            $data_migrated = get_option( 'whp_data_migrated', false );
     179
     180            if ( ! $data_migrated ) {
     181                // Fallback to meta
     182                $query->set( 'meta_key', $meta_key );
    109183                $query->set( 'meta_compare', 'NOT EXISTS' );
    110184            }
    111 
    112             // Hide on homepage.
    113             if ( ( is_front_page() && is_home() ) || is_front_page() ) {
    114                 $query->set( 'meta_key', '_whp_hide_on_frontpage' );
    115                 $query->set( 'meta_compare', 'NOT EXISTS' );
    116             } elseif ( is_home() ) {
    117                 // Hide on static blog page.
    118                 $query->set( 'meta_key', '_whp_hide_on_blog_page' );
    119                 $query->set( 'meta_compare', 'NOT EXISTS' );
    120             }
    121 
    122             // Hide on cpt archive.
    123             if ( is_post_type_archive( $q_post_type ) ) {
    124                 $query->set( 'meta_key', '_whp_hide_on_cpt_archive' );
    125                 $query->set( 'meta_compare', 'NOT EXISTS' );
    126             } elseif ( is_archive( $q_post_type ) ) {
    127                 // Hide on Archive.
    128                 $query->set( 'meta_key', '_whp_hide_on_archive' );
    129                 $query->set( 'meta_compare', 'NOT EXISTS' );
    130             }
    131 
    132             // Hide on Categories.
    133             if ( is_category() ) {
    134                 $query->set( 'meta_key', '_whp_hide_on_categories' );
    135                 $query->set( 'meta_compare', 'NOT EXISTS' );
    136             }
    137 
    138             // Hide on Search.
    139             if ( is_search() ) {
    140                 $query->set( 'meta_key', '_whp_hide_on_search' );
    141                 $query->set( 'meta_compare', 'NOT EXISTS' );
    142             }
    143 
    144             // Hide on Tags.
    145             if ( is_tag() ) {
    146                 $query->set( 'meta_key', '_whp_hide_on_tags' );
    147                 $query->set( 'meta_compare', 'NOT EXISTS' );
    148             }
    149 
    150             // Hide on Authors.
    151             if ( is_author() ) {
    152                 $query->set( 'meta_key', '_whp_hide_on_authors' );
    153                 $query->set( 'meta_compare', 'NOT EXISTS' );
    154             }
    155 
    156             // Hide on Date.
    157             if ( is_date() ) {
    158                 $query->set( 'meta_key', '_whp_hide_on_date' );
    159                 $query->set( 'meta_compare', 'NOT EXISTS' );
    160             }
    161 
    162             // Hide on RSS Feed.
    163             if ( is_feed() ) {
    164                 $query->set( 'meta_key', '_whp_hide_in_rss_feed' );
    165                 $query->set( 'meta_compare', 'NOT EXISTS' );
    166             }
    167 
    168             // Hide in Store.
    169             if ( whp_plugin()->is_woocommerce_active() && is_shop() ) {
    170                 $query->set( 'meta_key', '_whp_hide_on_store' );
    171                 $query->set( 'meta_compare', 'NOT EXISTS' );
    172             }
    173 
    174             // Hide on Product categories.
    175             if ( whp_plugin()->is_woocommerce_active() && is_product_category() ) {
    176                 $query->set( 'meta_key', '_whp_hide_on_product_category' );
    177                 $query->set( 'meta_compare', 'NOT EXISTS' );
    178             }
    179185        }
    180186    }
     
    188194     */
    189195    public function hide_from_post_navigation( $where ) {
    190         $hidden_on_post_navigation = whp_plugin()->get_hidden_posts_ids( 'post', 'post_navigation' );
     196        $data_migrated = get_option( 'whp_data_migrated', false );
     197
     198        $fallback = ! $data_migrated;
     199
     200        $hidden_on_post_navigation = whp_plugin()->get_hidden_posts_ids( 'post', 'post_navigation', $fallback );
    191201
    192202        if ( empty( $hidden_on_post_navigation ) ) {
     
    212222     */
    213223    public function hide_from_recent_post_widget( $query_args ) {
    214         $hidden_on_recent_posts = whp_plugin()->get_hidden_posts_ids( 'post', 'recent_posts' );
     224        $data_migrated = get_option( 'whp_data_migrated', false );
     225
     226        $fallback = ! $data_migrated;
     227
     228        $hidden_on_recent_posts = whp_plugin()->get_hidden_posts_ids( 'post', 'recent_posts', $fallback );
    215229
    216230        if ( empty( $hidden_on_recent_posts ) ) {
  • whp-hide-posts/trunk/inc/core/class-plugin.php

    r2674320 r3213937  
    4848     * @param   string $post_type  The post type to be filtered.
    4949     * @param   string $from Filter for the posts hidden on specific page.
     50     * @param   boolean $fallback Should it fallback to meta table.
    5051     *
    5152     * @return  array
    5253     */
    53     public function get_hidden_posts_ids( $post_type = 'post', $from = 'all' ) {
     54    public function get_hidden_posts_ids( $post_type = 'post', $from = 'all', $fallback = false ) {
    5455        $cache_key = 'whp_' . $post_type . '_' . $from;
    5556
     
    7273        }
    7374
    74         global $wpdb;
    75 
    76         $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
     75        $key = str_replace( '_whp_', '', $key );
     76
     77        global $wpdb;
     78
     79        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     80
     81        $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$table_name} WHERE `condition` = %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
    7782
    7883        if ( 'all' === $key ) {
    79             $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
     84            $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$table_name} WHERE `condition` LIKE %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
    8085        }
    8186
    8287        $hidden_posts = $wpdb->get_col( $sql );
     88
     89        if ( empty( $hidden_posts ) && $fallback ) {
     90            $key = '_whp_' . $key;
     91
     92            $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
     93
     94            if ( 'all' === $key ) {
     95                $sql = $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", $key, $post_type );
     96            }
     97
     98            $hidden_posts = $wpdb->get_col( $sql );
     99        }
    83100
    84101        wp_cache_set( $cache_key, $hidden_posts, 'whp' );
     
    138155        return in_array( $current_post_type, $custom_types, true );
    139156    }
     157
     158    /**
     159     * Check if post is hidden in the custom table
     160     *
     161     * @param  int  $post_id  The post id.
     162     * @param  string  $key      The key name.
     163     * @param  boolean $fallback Should it check in the post meta table.
     164     *
     165     * @return boolean
     166     */
     167    public function get_whp_meta( $post_id, $key, $fallback = false ) {
     168        global $wpdb;
     169
     170        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     171
     172        $hidden_post = (int) $wpdb->get_var(
     173            $wpdb->prepare(
     174                "SELECT COUNT(*) FROM {$table_name} WHERE post_id = %d AND `condition` = %s",
     175                $post_id,
     176                $key
     177            )
     178        );
     179
     180        if ( $hidden_post ) {
     181            return true;
     182        }
     183
     184        if ( $fallback ) {
     185            return get_post_meta( $post_id, '_whp_' . $key, true );
     186        }
     187
     188        return false;
     189    }
     190
     191    /**
     192     * Set post for hiding
     193     *
     194     * @param  int    $post_id  The post id.
     195     * @param  string $key      The key name.
     196     *
     197     * @return boolean
     198     */
     199    public function add_whp_meta( $post_id, $key, $fallback = false ) {
     200        global $wpdb;
     201
     202        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     203
     204        $wpdb->insert(
     205            $table_name,
     206            array(
     207                'post_id' => $post_id,
     208                'condition' => $key,
     209            ),
     210            array(
     211                '%d',
     212                '%s',
     213            )
     214        );
     215    }
     216
     217    /**
     218     * Remove post from hiding
     219     *
     220     * @param  int    $post_id  The post id.
     221     * @param  string $key      The key name.
     222     *
     223     * @return boolean
     224     */
     225    public function delete_whp_meta( $post_id, $key, $fallback = false ) {
     226        global $wpdb;
     227
     228        $table_name = $wpdb->prefix . 'whp_posts_visibility';
     229
     230        $wpdb->delete(
     231            $table_name,
     232            array(
     233                'post_id' => $post_id,
     234                'condition' => $key,
     235            ),
     236            array(
     237                '%d',
     238                '%s',
     239            )
     240        );
     241
     242        delete_post_meta( $post_id, '_whp_' . $key );
     243    }
    140244}
  • whp-hide-posts/trunk/uninstall.php

    r2674320 r3213937  
    1313
    1414global $wpdb;
    15 $query = "DELETE FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '_whp_hide_on_%'";
    16 $wpdb->query( $query );
     15$table_name = $wpdb->prefix . 'whp_posts_visibility';
     16$wpdb->query( "DELETE FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '_whp_hide_on_%'" );
     17$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
  • whp-hide-posts/trunk/whp-hide-posts.php

    r2794480 r3213937  
    55 * Author:      MartinCV
    66 * Author URI:  https://www.martincv.com
    7  * Version:     1.1.1
     7 * Version:     2.0
    88 * Text Domain: whp-hide-posts
    99 *
     
    4949     * @var string
    5050     */
    51     private $version = '1.1.1';
     51    private $version = '2.0';
    5252
    5353    /**
     
    114114
    115115        \MartinCV\WHP\Zeen_Theme::get_instance();
     116        \MartinCV\WHP\Core\Database::get_instance()->create_tables();
    116117
    117118        // Init classes if is Admin/Dashboard.
Note: See TracChangeset for help on using the changeset viewer.