| 1 |
<?php |
| 2 |
/** |
| 3 |
* Database handling for WP Post Hide |
| 4 |
* |
| 5 |
* @package WP Post Hide |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if directly access. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Database management class |
| 15 |
*/ |
| 16 |
class XSWPHP_Database { |
| 17 |
|
| 18 |
/** |
| 19 |
* Create database tables |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public static function create_tables() { |
| 24 |
$current_db_version = 1; |
| 25 |
$db_version = get_option( 'xswphp_db_version', 0 ); |
| 26 |
|
| 27 |
if ( $current_db_version === (int) $db_version ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
$xswphp_posts_visibility_table = $wpdb->prefix . 'xswphp_posts_visibility'; |
| 34 |
$charset_collate = $wpdb->get_charset_collate(); |
| 35 |
|
| 36 |
$xswphp_posts_visibility = "CREATE TABLE $xswphp_posts_visibility_table ( |
| 37 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 38 |
post_id BIGINT(20) UNSIGNED NOT NULL, |
| 39 |
`condition` VARCHAR(100) NOT NULL, |
| 40 |
PRIMARY KEY (id), |
| 41 |
INDEX pid_con (post_id,`condition`) |
| 42 |
) $charset_collate;"; |
| 43 |
|
| 44 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 45 |
|
| 46 |
dbDelta( $xswphp_posts_visibility ); |
| 47 |
|
| 48 |
update_option( 'xswphp_db_version', $current_db_version ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get hidden post IDs with caching |
| 53 |
* |
| 54 |
* @param string $post_type The post type. |
| 55 |
* @param string $condition The hide condition. |
| 56 |
* @param boolean $fallback Should it fallback to meta table. |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public static function get_hidden_posts_ids( $post_type = 'post', $condition = 'all', $fallback = true ) { |
| 60 |
$cache_key = 'xswphp_' . $post_type . '_' . $condition; |
| 61 |
|
| 62 |
$hidden_posts = wp_cache_get( $cache_key, 'xswphp' ); |
| 63 |
if ( false !== $hidden_posts ) { |
| 64 |
return $hidden_posts; |
| 65 |
} |
| 66 |
|
| 67 |
$hidden_posts = get_transient( $cache_key ); |
| 68 |
if ( false !== $hidden_posts ) { |
| 69 |
wp_cache_set( $cache_key, $hidden_posts, 'xswphp' ); |
| 70 |
return $hidden_posts; |
| 71 |
} |
| 72 |
|
| 73 |
global $wpdb; |
| 74 |
$table_name = esc_sql( $wpdb->prefix . 'xswphp_posts_visibility' ); |
| 75 |
|
| 76 |
if ( 'all' === $condition ) { |
| 77 |
$sql = $wpdb->prepare( |
| 78 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 79 |
"SELECT DISTINCT post_id FROM {$table_name} WHERE post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", |
| 80 |
$post_type |
| 81 |
); |
| 82 |
} else { |
| 83 |
$sql = $wpdb->prepare( |
| 84 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 85 |
"SELECT DISTINCT post_id FROM {$table_name} WHERE `condition` = %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", |
| 86 |
$condition, |
| 87 |
$post_type |
| 88 |
); |
| 89 |
} |
| 90 |
// phpcs:ignore WordPress.DB |
| 91 |
$hidden_posts = $wpdb->get_col( $sql ); |
| 92 |
|
| 93 |
if ( empty( $hidden_posts ) && $fallback ) { |
| 94 |
// Fallback to meta table. |
| 95 |
$meta_key = self::get_meta_key_from_condition( $condition ); |
| 96 |
if ( $meta_key ) { |
| 97 |
$sql = $wpdb->prepare( |
| 98 |
"SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = %s)", |
| 99 |
$meta_key, |
| 100 |
$post_type |
| 101 |
); |
| 102 |
// phpcs:ignore WordPress.DB |
| 103 |
$hidden_posts = $wpdb->get_col( $sql ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! is_array( $hidden_posts ) ) { |
| 108 |
$hidden_posts = array(); |
| 109 |
} |
| 110 |
|
| 111 |
wp_cache_set( $cache_key, $hidden_posts, 'xswphp' ); |
| 112 |
set_transient( $cache_key, $hidden_posts, WEEK_IN_SECONDS ); |
| 113 |
|
| 114 |
return $hidden_posts; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Check if post is hidden in custom table |
| 119 |
* |
| 120 |
* @param int $post_id The post id. |
| 121 |
* @param string $condition The condition. |
| 122 |
* @param boolean $fallback Should check meta table. |
| 123 |
* @return boolean |
| 124 |
*/ |
| 125 |
public static function is_post_hidden( $post_id, $condition, $fallback = true ) { |
| 126 |
global $wpdb; |
| 127 |
$table_name = esc_sql( $wpdb->prefix . 'xswphp_posts_visibility' ); |
| 128 |
// phpcs:ignore WordPress.DB |
| 129 |
$hidden_post = (int) $wpdb->get_var( |
| 130 |
$wpdb->prepare( |
| 131 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 132 |
"SELECT COUNT(*) FROM {$table_name} WHERE post_id = %d AND `condition` = %s", |
| 133 |
$post_id, |
| 134 |
$condition |
| 135 |
) |
| 136 |
); |
| 137 |
|
| 138 |
if ( $hidden_post ) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
if ( $fallback ) { |
| 143 |
$meta_key = self::get_meta_key_from_condition( $condition ); |
| 144 |
if ( $meta_key ) { |
| 145 |
return get_post_meta( $post_id, $meta_key, true ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Add hiding condition to database |
| 154 |
* |
| 155 |
* @param int $post_id The post id. |
| 156 |
* @param string $condition The condition. |
| 157 |
* @return boolean |
| 158 |
*/ |
| 159 |
public static function add_hide_condition( $post_id, $condition ) { |
| 160 |
global $wpdb; |
| 161 |
$table_name = $wpdb->prefix . 'xswphp_posts_visibility'; |
| 162 |
// phpcs:ignore WordPress.DB |
| 163 |
$result = $wpdb->insert( |
| 164 |
$table_name, |
| 165 |
array( |
| 166 |
'post_id' => $post_id, |
| 167 |
'condition' => $condition, |
| 168 |
), |
| 169 |
array( |
| 170 |
'%d', |
| 171 |
'%s', |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
return false !== $result; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Remove hiding condition from database |
| 180 |
* |
| 181 |
* @param int $post_id The post id. |
| 182 |
* @param string $condition The condition. |
| 183 |
* @return boolean |
| 184 |
*/ |
| 185 |
public static function remove_hide_condition( $post_id, $condition ) { |
| 186 |
global $wpdb; |
| 187 |
$table_name = $wpdb->prefix . 'xswphp_posts_visibility'; |
| 188 |
// phpcs:ignore WordPress.DB |
| 189 |
$result = $wpdb->delete( |
| 190 |
$table_name, |
| 191 |
array( |
| 192 |
'post_id' => $post_id, |
| 193 |
'condition' => $condition, |
| 194 |
), |
| 195 |
array( |
| 196 |
'%d', |
| 197 |
'%s', |
| 198 |
) |
| 199 |
); |
| 200 |
|
| 201 |
// Also remove from post meta for cleanup. |
| 202 |
$meta_key = self::get_meta_key_from_condition( $condition ); |
| 203 |
if ( $meta_key ) { |
| 204 |
delete_post_meta( $post_id, $meta_key ); |
| 205 |
} |
| 206 |
|
| 207 |
return false !== $result; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Clear cache for post |
| 212 |
* |
| 213 |
* @param int $post_id The post id. |
| 214 |
* @param string $post_type The post type. |
| 215 |
*/ |
| 216 |
public static function clear_post_cache( $post_id, $post_type = null ) { |
| 217 |
if ( ! $post_type ) { |
| 218 |
$post_type = get_post_type( $post_id ); |
| 219 |
} |
| 220 |
|
| 221 |
$conditions = array( |
| 222 |
'all', |
| 223 |
'front_page', |
| 224 |
'blog_page', |
| 225 |
'category_page', |
| 226 |
'tag_page', |
| 227 |
'author', |
| 228 |
'archive', |
| 229 |
'search', |
| 230 |
'feeds', |
| 231 |
'recent', |
| 232 |
'rel_link', |
| 233 |
'redirect_page', |
| 234 |
'always', |
| 235 |
'keep_search', |
| 236 |
'rest_api', |
| 237 |
'single_post_page', |
| 238 |
); |
| 239 |
|
| 240 |
foreach ( $conditions as $condition ) { |
| 241 |
$cache_key = 'xswphp_' . $post_type . '_' . $condition; |
| 242 |
wp_cache_delete( $cache_key, 'xswphp' ); |
| 243 |
delete_transient( $cache_key ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Get meta key from condition |
| 249 |
* |
| 250 |
* @param string $condition Conditon meta key. |
| 251 |
* @return string|false |
| 252 |
*/ |
| 253 |
private static function get_meta_key_from_condition( $condition ) { |
| 254 |
$mapping = array( |
| 255 |
'front_page' => '_xswphp_front_page', |
| 256 |
'category_page' => '_xswphp_category_page', |
| 257 |
'tag_page' => '_xswphp_tag_page', |
| 258 |
'author' => '_xswphp_author', |
| 259 |
'archive' => '_xswphp_archive', |
| 260 |
'search' => '_xswphp_search', |
| 261 |
'feeds' => '_xswphp_feeds', |
| 262 |
'recent' => '_xswphp_recent', |
| 263 |
'rel_link' => '_xswphp_rel_link', |
| 264 |
'redirect_page' => '_xswphp_redirect_page', |
| 265 |
'always' => '_xswphp_always', |
| 266 |
'keep_search' => '_xswphp_keep_search', |
| 267 |
'rest_api' => '_xswphp_rest_api', |
| 268 |
); |
| 269 |
|
| 270 |
return isset( $mapping[ $condition ] ) ? $mapping[ $condition ] : false; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Migrate data from meta to table |
| 275 |
*/ |
| 276 |
public static function migrate_meta_to_table() { |
| 277 |
$data_migrated = get_option( 'xswphp_data_migrated', false ); |
| 278 |
if ( $data_migrated ) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
global $wpdb; |
| 283 |
$table_name = $wpdb->prefix . 'xswphp_posts_visibility'; |
| 284 |
|
| 285 |
$meta_keys = array( |
| 286 |
'_xswphp_front_page' => 'front_page', |
| 287 |
'_xswphp_category_page' => 'category_page', |
| 288 |
'_xswphp_tag_page' => 'tag_page', |
| 289 |
'_xswphp_author' => 'author', |
| 290 |
'_xswphp_archive' => 'archive', |
| 291 |
'_xswphp_search' => 'search', |
| 292 |
'_xswphp_feeds' => 'feeds', |
| 293 |
'_xswphp_recent' => 'recent', |
| 294 |
'_xswphp_rel_link' => 'rel_link', |
| 295 |
'_xswphp_redirect_page' => 'redirect_page', |
| 296 |
'_xswphp_always' => 'always', |
| 297 |
'_xswphp_keep_search' => 'keep_search', |
| 298 |
'_xswphp_rest_api' => 'rest_api', |
| 299 |
); |
| 300 |
|
| 301 |
foreach ( $meta_keys as $meta_key => $condition ) { |
| 302 |
// phpcs:ignore WordPress.DB |
| 303 |
$posts = $wpdb->get_results( |
| 304 |
$wpdb->prepare( |
| 305 |
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value != ''", |
| 306 |
$meta_key |
| 307 |
) |
| 308 |
); |
| 309 |
|
| 310 |
foreach ( $posts as $post ) { |
| 311 |
$exist = self::is_post_hidden( $post->post_id, $condition, false ); |
| 312 |
if ( ! $exist ) { |
| 313 |
self::add_hide_condition( $post->post_id, $condition ); |
| 314 |
} |
| 315 |
delete_post_meta( $post->post_id, $meta_key ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
update_option( 'xswphp_data_migrated', true ); |
| 320 |
} |
| 321 |
} |
| 322 |
|