| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WP_Post; |
| 6 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for post related things. |
| 10 |
*/ |
| 11 |
class Post_Helper { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the String_Helper instance. |
| 15 |
* |
| 16 |
* @var String_Helper |
| 17 |
*/ |
| 18 |
private $string; |
| 19 |
|
| 20 |
/** |
| 21 |
* Represents the indexables repository. |
| 22 |
* |
| 23 |
* @var Indexable_Repository |
| 24 |
*/ |
| 25 |
private $repository; |
| 26 |
|
| 27 |
/** |
| 28 |
* Post_Helper constructor. |
| 29 |
* |
| 30 |
* @codeCoverageIgnore It only sets dependencies. |
| 31 |
* |
| 32 |
* @param String_Helper $string_helper The string helper. |
| 33 |
*/ |
| 34 |
public function __construct( String_Helper $string_helper ) { |
| 35 |
$this->string = $string_helper; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets the indexable repository. Done to avoid circular dependencies. |
| 40 |
* |
| 41 |
* @required |
| 42 |
* |
| 43 |
* @param Indexable_Repository $repository The indexable repository. |
| 44 |
*/ |
| 45 |
public function set_indexable_repository( Indexable_Repository $repository ) { |
| 46 |
$this->repository = $repository; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Removes all shortcode tags from the given content. |
| 51 |
* |
| 52 |
* @codeCoverageIgnore It only wraps a WordPress function. |
| 53 |
* |
| 54 |
* @param string $content Content to remove all the shortcode tags from. |
| 55 |
* |
| 56 |
* @return string Content without shortcode tags. |
| 57 |
*/ |
| 58 |
public function strip_shortcodes( $content ) { |
| 59 |
return \strip_shortcodes( $content ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Retrieves the post excerpt (without tags). |
| 64 |
* |
| 65 |
* @codeCoverageIgnore It only wraps another helper method. |
| 66 |
* |
| 67 |
* @param int $post_id Post ID. |
| 68 |
* |
| 69 |
* @return string Post excerpt (without tags). |
| 70 |
*/ |
| 71 |
public function get_the_excerpt( $post_id ) { |
| 72 |
return $this->string->strip_all_tags( \get_the_excerpt( $post_id ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Retrieves the post type of the current post. |
| 77 |
* |
| 78 |
* @codeCoverageIgnore It only wraps a WordPress function. |
| 79 |
* |
| 80 |
* @param WP_Post|null $post The post. |
| 81 |
* |
| 82 |
* @return string|false Post type on success, false on failure. |
| 83 |
*/ |
| 84 |
public function get_post_type( $post = null ) { |
| 85 |
return \get_post_type( $post ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Retrieves the post title with fallback to `No title`. |
| 90 |
* |
| 91 |
* @param int $post_id Optional. Post ID. |
| 92 |
* |
| 93 |
* @return string The post title with fallback to `No title`. |
| 94 |
*/ |
| 95 |
public function get_post_title_with_fallback( $post_id = 0 ) { |
| 96 |
$post_title = \get_the_title( $post_id ); |
| 97 |
if ( $post_title ) { |
| 98 |
return $post_title; |
| 99 |
} |
| 100 |
|
| 101 |
return \__( 'No title', 'wordpress-seo' ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Retrieves post data given a post ID. |
| 106 |
* |
| 107 |
* @codeCoverageIgnore It wraps a WordPress function. |
| 108 |
* |
| 109 |
* @param int $post_id Post ID. |
| 110 |
* |
| 111 |
* @return WP_Post|null The post. |
| 112 |
*/ |
| 113 |
public function get_post( $post_id ) { |
| 114 |
return \get_post( $post_id ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Updates the has_public_posts field on attachments for a post_parent. |
| 119 |
* |
| 120 |
* An attachment is represented by their post parent when: |
| 121 |
* - The attachment has a post parent. |
| 122 |
* - The attachment inherits the post status. |
| 123 |
* |
| 124 |
* @codeCoverageIgnore It relies too much on dependencies. |
| 125 |
* |
| 126 |
* @param int $post_parent Post ID. |
| 127 |
* @param int $has_public_posts Whether the parent is public. |
| 128 |
* |
| 129 |
* @return bool Whether the update was successful. |
| 130 |
*/ |
| 131 |
public function update_has_public_posts_on_attachments( $post_parent, $has_public_posts ) { |
| 132 |
$query = $this->repository->query() |
| 133 |
->select( 'id' ) |
| 134 |
->where( 'object_type', 'post' ) |
| 135 |
->where( 'object_sub_type', 'attachment' ) |
| 136 |
->where( 'post_status', 'inherit' ) |
| 137 |
->where( 'post_parent', $post_parent ); |
| 138 |
|
| 139 |
if ( $has_public_posts !== null ) { |
| 140 |
$query->where_raw( '( has_public_posts IS NULL OR has_public_posts <> %s )', [ $has_public_posts ] ); |
| 141 |
} |
| 142 |
else { |
| 143 |
$query->where_not_null( 'has_public_posts' ); |
| 144 |
} |
| 145 |
$results = $query->find_array(); |
| 146 |
|
| 147 |
if ( empty( $results ) ) { |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
$updated = $this->repository->query() |
| 152 |
->set( 'has_public_posts', $has_public_posts ) |
| 153 |
->where_id_in( \wp_list_pluck( $results, 'id' ) ) |
| 154 |
->update_many(); |
| 155 |
|
| 156 |
return $updated !== false; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Determines if the post can be indexed. |
| 161 |
* |
| 162 |
* @param int $post_id Post ID to check. |
| 163 |
* |
| 164 |
* @return bool True if the post can be indexed. |
| 165 |
*/ |
| 166 |
public function is_post_indexable( $post_id ) { |
| 167 |
// Don't index excluded post statuses. |
| 168 |
if ( \in_array( \get_post_status( $post_id ), $this->get_excluded_post_statuses(), true ) ) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
// Don't index revisions of posts. |
| 173 |
if ( \wp_is_post_revision( $post_id ) ) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
// Don't index autosaves that are not caught by the auto-draft check. |
| 178 |
if ( \wp_is_post_autosave( $post_id ) ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Retrieves the list of excluded post statuses. |
| 187 |
* |
| 188 |
* @return array The excluded post statuses. |
| 189 |
*/ |
| 190 |
public function get_excluded_post_statuses() { |
| 191 |
return [ 'auto-draft' ]; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Retrieves the list of public posts statuses. |
| 196 |
* |
| 197 |
* @return array The public post statuses. |
| 198 |
*/ |
| 199 |
public function get_public_post_statuses() { |
| 200 |
/** |
| 201 |
* Filter: 'wpseo_public_post_statuses' - List of public post statuses. |
| 202 |
* |
| 203 |
* @api array $post_statuses Post status list, defaults to array( 'publish' ). |
| 204 |
*/ |
| 205 |
return \apply_filters( 'wpseo_public_post_statuses', [ 'publish' ] ); |
| 206 |
} |
| 207 |
} |
| 208 |
|