| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Posts class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services\Import; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* This class responsible for querying and updating the posts. |
| 13 |
*/ |
| 14 |
class Post |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Returns all the posts needed for the update. |
| 18 |
* |
| 19 |
* @return mixed |
| 20 |
*/ |
| 21 |
public static function all() |
| 22 |
{ |
| 23 |
$wpdb = $GLOBALS['wpdb']; |
| 24 |
|
| 25 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 26 |
return $wpdb->get_results( |
| 27 |
$wpdb->prepare( |
| 28 |
"SELECT * FROM {$wpdb->posts} WHERE post_status != 'trash' |
| 29 |
AND (INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0) |
| 30 |
AND post_type NOT IN ('revision', 'attachment')", // phpcs:ignoreFile Generic.Files.LineLength.TooLong |
| 31 |
'extendify-image-import', |
| 32 |
' ext-import"', |
| 33 |
' ext-import ', |
| 34 |
'"ext-import ' |
| 35 |
) |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns all the posts needed for the update. |
| 41 |
* |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public static function countPostsNeedingUpdate() |
| 45 |
{ |
| 46 |
$wpdb = $GLOBALS['wpdb']; |
| 47 |
|
| 48 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 49 |
return $wpdb->get_row( |
| 50 |
$wpdb->prepare( |
| 51 |
"SELECT count(ID) as posts_count FROM {$wpdb->posts} WHERE post_status != 'trash' |
| 52 |
AND (INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0 OR INSTR(post_content, %s) > 0) |
| 53 |
AND post_type NOT IN ('revision', 'attachment')", // phpcs:ignoreFile Generic.Files.LineLength.TooLong |
| 54 |
'extendify-image-import', |
| 55 |
' ext-import"', |
| 56 |
' ext-import ', |
| 57 |
'"ext-import ' |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check if the post is not being edited at the moment. |
| 64 |
* |
| 65 |
* @param int $postId the WordPress post id. |
| 66 |
* @return bool |
| 67 |
*/ |
| 68 |
public static function isLocked($postId) |
| 69 |
{ |
| 70 |
require_once ABSPATH . '/wp-admin/includes/post.php'; |
| 71 |
return wp_check_post_lock($postId) !== false; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Update the post with the new content |
| 76 |
* |
| 77 |
* @param \WP_Post $post the WordPress post. |
| 78 |
* @param string $content the new content of the post. |
| 79 |
* @return int|\WP_Error The post ID on success. The value 0 or WP_Error on failure. |
| 80 |
*/ |
| 81 |
public static function update($post, $content) |
| 82 |
{ |
| 83 |
/** |
| 84 |
* Compare the saved modification time in the database |
| 85 |
* with the one we have for the post, if they are not identical |
| 86 |
* we abort the update completely. |
| 87 |
*/ |
| 88 |
$currentUpdatedAt = get_post_field('post_modified', $post->ID); |
| 89 |
if (strtotime($currentUpdatedAt) !== strtotime($post->post_modified)) { |
| 90 |
return new \WP_Error(1006, 'Post has been modified.'); |
| 91 |
} |
| 92 |
|
| 93 |
// Recheck the database to ensure that the post is not being edited at the moment. |
| 94 |
if (self::isLocked($post->ID)) { |
| 95 |
return new \WP_Error(1005, 'Post is locked.'); |
| 96 |
} |
| 97 |
|
| 98 |
$updatedPost = [ |
| 99 |
'ID' => $post->ID, |
| 100 |
'post_content' => $content, |
| 101 |
]; |
| 102 |
// Prevent re-sanitizing the content. |
| 103 |
remove_filter('content_save_pre', 'wp_filter_post_kses'); |
| 104 |
$id = wp_update_post($updatedPost, true); |
| 105 |
add_filter('content_save_pre', 'wp_filter_post_kses'); |
| 106 |
return $id; |
| 107 |
} |
| 108 |
} |
| 109 |
|