| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parsely class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 2.5.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely; |
| 12 |
|
| 13 |
use WP_Post; |
| 14 |
use WP_User; |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds most of the logic for the plugin. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @since 2.5.0 Moved from plugin root file to this file. |
| 21 |
*/ |
| 22 |
class Parsely { |
| 23 |
/** |
| 24 |
* Declare our constants |
| 25 |
*/ |
| 26 |
public const VERSION = PARSELY_VERSION; |
| 27 |
public const MENU_SLUG = 'parsely'; // Defines the page param passed to options-general.php. |
| 28 |
public const OPTIONS_KEY = 'parsely'; // Defines the key used to store options in the WP database. |
| 29 |
public const CAPABILITY = 'manage_options'; // The capability required for the user to administer settings. |
| 30 |
|
| 31 |
/** |
| 32 |
* Declare some class properties |
| 33 |
* |
| 34 |
* @var array $option_defaults The defaults we need for the class. |
| 35 |
*/ |
| 36 |
private $option_defaults = array( |
| 37 |
'apikey' => '', |
| 38 |
'content_id_prefix' => '', |
| 39 |
'api_secret' => '', |
| 40 |
'use_top_level_cats' => false, |
| 41 |
'custom_taxonomy_section' => 'category', |
| 42 |
'cats_as_tags' => false, |
| 43 |
'track_authenticated_users' => true, |
| 44 |
'lowercase_tags' => true, |
| 45 |
'force_https_canonicals' => false, |
| 46 |
'track_post_types' => array( 'post' ), |
| 47 |
'track_page_types' => array( 'page' ), |
| 48 |
'disable_javascript' => false, |
| 49 |
'disable_amp' => false, |
| 50 |
'meta_type' => 'json_ld', |
| 51 |
'logo' => '', |
| 52 |
'metadata_secret' => '', |
| 53 |
'parsely_wipe_metadata_cache' => false, |
| 54 |
); |
| 55 |
|
| 56 |
/** |
| 57 |
* Declare post types that Parse.ly will process as "posts". |
| 58 |
* |
| 59 |
* @link https://www.parse.ly/help/integration/jsonld#distinguishing-between-posts-and-pages |
| 60 |
* |
| 61 |
* @since 2.5.0 |
| 62 |
* @var string[] |
| 63 |
*/ |
| 64 |
private $supported_jsonld_post_types = array( |
| 65 |
'NewsArticle', |
| 66 |
'Article', |
| 67 |
'TechArticle', |
| 68 |
'BlogPosting', |
| 69 |
'LiveBlogPosting', |
| 70 |
'Report', |
| 71 |
'Review', |
| 72 |
'CreativeWork', |
| 73 |
); |
| 74 |
|
| 75 |
/** |
| 76 |
* Declare post types that Parse.ly will process as "non-posts". |
| 77 |
* |
| 78 |
* @link https://www.parse.ly/help/integration/jsonld#distinguishing-between-posts-and-pages |
| 79 |
* |
| 80 |
* @since 2.5.0 |
| 81 |
* @var string[] |
| 82 |
*/ |
| 83 |
private $supported_jsonld_non_post_types = array( |
| 84 |
'WebPage', |
| 85 |
'Event', |
| 86 |
'Hotel', |
| 87 |
'Restaurant', |
| 88 |
'Movie', |
| 89 |
); |
| 90 |
|
| 91 |
/** |
| 92 |
* Register action and filter hook callbacks. |
| 93 |
* |
| 94 |
* Also, immediately upgrade options if needed. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function run(): void { |
| 99 |
// Run upgrade options if they exist for the version currently defined. |
| 100 |
$options = $this->get_options(); |
| 101 |
if ( empty( $options['plugin_version'] ) || self::VERSION !== $options['plugin_version'] ) { |
| 102 |
$method = 'upgrade_plugin_to_version_' . str_replace( '.', '_', self::VERSION ); |
| 103 |
if ( method_exists( $this, $method ) ) { |
| 104 |
call_user_func_array( array( $this, $method ), array( $options ) ); |
| 105 |
} |
| 106 |
// Update our version info. |
| 107 |
$options['plugin_version'] = self::VERSION; |
| 108 |
update_option( self::OPTIONS_KEY, $options ); |
| 109 |
} |
| 110 |
|
| 111 |
// phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval |
| 112 |
add_filter( 'cron_schedules', array( $this, 'wpparsely_add_cron_interval' ) ); |
| 113 |
add_action( 'parsely_bulk_metas_update', array( $this, 'bulk_update_posts' ) ); |
| 114 |
add_action( 'save_post', array( $this, 'update_metadata_endpoint' ) ); |
| 115 |
add_action( 'wp_head', array( $this, 'insert_page_header_metadata' ) ); |
| 116 |
add_action( 'wp_enqueue_scripts', array( $this, 'wp_parsely_style_init' ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Adds 10 minute cron interval. |
| 121 |
* |
| 122 |
* @param array $schedules WP schedules array. |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public function wpparsely_add_cron_interval( array $schedules ): array { |
| 126 |
$schedules['everytenminutes'] = array( |
| 127 |
'interval' => 600, // time in seconds. |
| 128 |
'display' => __( 'Every 10 Minutes', 'wp-parsely' ), |
| 129 |
); |
| 130 |
return $schedules; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Initialize Parse.ly WordPress style. |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function wp_parsely_style_init(): void { |
| 139 |
wp_register_style( 'wp-parsely-style', plugin_dir_url( PARSELY_FILE ) . 'wp-parsely.css', array(), self::VERSION ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Actually inserts the code for the <meta name='parsely-page'> parameter within the <head></head> tag. |
| 144 |
* |
| 145 |
* @since 3.0.0 |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function insert_page_header_metadata(): void { |
| 150 |
/** |
| 151 |
* Filters whether the Parse.ly meta tags should be inserted in the page. |
| 152 |
* |
| 153 |
* By default, the tags are inserted. |
| 154 |
* |
| 155 |
* @since 3.0.0 |
| 156 |
* |
| 157 |
* @param bool $insert_metadata True to insert the metadata, false otherwise. |
| 158 |
*/ |
| 159 |
if ( ! apply_filters( 'wp_parsely_should_insert_metadata', true ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$parsely_options = $this->get_options(); |
| 164 |
|
| 165 |
if ( |
| 166 |
$this->api_key_is_missing() || |
| 167 |
|
| 168 |
// Chosen not to track logged-in users. |
| 169 |
( ! $parsely_options['track_authenticated_users'] && $this->parsely_is_user_logged_in() ) || |
| 170 |
|
| 171 |
// 404 pages are not tracked. |
| 172 |
is_404() || |
| 173 |
|
| 174 |
// Search pages are not tracked. |
| 175 |
is_search() |
| 176 |
) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
global $post; |
| 181 |
|
| 182 |
// We can't construct the metadata without a valid post object. |
| 183 |
if ( empty( $post ) ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
// Assign default values for LD+JSON |
| 188 |
// TODO: Mapping of an install's post types to Parse.ly post types (namely page/post). |
| 189 |
$parsely_page = $this->construct_parsely_metadata( $parsely_options, $post ); |
| 190 |
|
| 191 |
// Something went wrong - abort. |
| 192 |
if ( empty( $parsely_page ) || ! isset( $parsely_page['headline'] ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
echo "\n" . '<!-- BEGIN Parse.ly ' . esc_html( self::VERSION ) . ' -->' . "\n"; |
| 197 |
|
| 198 |
// Insert JSON-LD or repeated metas. |
| 199 |
if ( 'json_ld' === $parsely_options['meta_type'] ) { |
| 200 |
include plugin_dir_path( PARSELY_FILE ) . 'views/json-ld.php'; |
| 201 |
} else { |
| 202 |
// Assume `meta_type` is `repeated_metas`. |
| 203 |
$parsely_post_type = $this->convert_jsonld_to_parsely_type( $parsely_page['@type'] ); |
| 204 |
if ( isset( $parsely_page['keywords'] ) && is_array( $parsely_page['keywords'] ) ) { |
| 205 |
$parsely_page['keywords'] = implode( ',', $parsely_page['keywords'] ); |
| 206 |
} |
| 207 |
|
| 208 |
$parsely_metas = array( |
| 209 |
'title' => $parsely_page['headline'] ?? null, |
| 210 |
'link' => $parsely_page['url'] ?? null, |
| 211 |
'type' => $parsely_post_type, |
| 212 |
'image-url' => $parsely_page['thumbnailUrl'] ?? null, |
| 213 |
'pub-date' => $parsely_page['datePublished'] ?? null, |
| 214 |
'section' => $parsely_page['articleSection'] ?? null, |
| 215 |
'tags' => $parsely_page['keywords'] ?? null, |
| 216 |
'author' => isset( $parsely_page['author'] ), |
| 217 |
); |
| 218 |
$parsely_metas = array_filter( $parsely_metas, array( $this, 'filter_empty_and_not_string_from_array' ) ); |
| 219 |
|
| 220 |
if ( isset( $parsely_page['author'] ) ) { |
| 221 |
$parsely_page_authors = wp_list_pluck( $parsely_page['author'], 'name' ); |
| 222 |
$parsely_page_authors = array_filter( $parsely_page_authors, array( $this, 'filter_empty_and_not_string_from_array' ) ); |
| 223 |
} |
| 224 |
|
| 225 |
include plugin_dir_path( PARSELY_FILE ) . 'views/repeated-metas.php'; |
| 226 |
} |
| 227 |
|
| 228 |
// Add any custom metadata. |
| 229 |
if ( isset( $parsely_page['custom_metadata'] ) ) { |
| 230 |
include plugin_dir_path( PARSELY_FILE ) . 'views/custom-metadata.php'; |
| 231 |
} |
| 232 |
|
| 233 |
echo '<!-- END Parse.ly -->' . "\n\n"; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Deprecated. Echoes the metadata into the page, and returns the inserted values. |
| 238 |
* |
| 239 |
* To just echo the metadata, use the `insert_page_header_metadata()` method. |
| 240 |
* To get the metadata to be inserted, use the `construct_parsely_metadata()` method. |
| 241 |
* |
| 242 |
* @deprecated 3.0.0 |
| 243 |
* @see construct_parsely_metadata() |
| 244 |
* |
| 245 |
* @return array |
| 246 |
*/ |
| 247 |
public function insert_parsely_page(): array { |
| 248 |
_deprecated_function( __FUNCTION__, '3.0', 'construct_parsely_metadata()' ); |
| 249 |
$this->insert_page_header_metadata(); |
| 250 |
|
| 251 |
global $post; |
| 252 |
return $this->construct_parsely_metadata( $this->get_options(), $post ); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Function to be used in `array_filter` to clean up repeated metas |
| 257 |
* |
| 258 |
* @param mixed $var Value to filter from the array. |
| 259 |
* @return bool Returns true if the variable is not empty, and it's a string |
| 260 |
*/ |
| 261 |
private static function filter_empty_and_not_string_from_array( $var ): bool { |
| 262 |
return ! empty( $var ) && is_string( $var ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Compare the post_status key against an allowed list (by default, only 'publish'ed content includes tracking data). |
| 267 |
* |
| 268 |
* @since 2.5.0 |
| 269 |
* |
| 270 |
* @param int|WP_Post $post Which post object or ID to check. |
| 271 |
* @return bool Should the post status be tracked for the provided post's post_type. By default, only 'publish' is allowed. |
| 272 |
*/ |
| 273 |
public static function post_has_trackable_status( $post ): bool { |
| 274 |
static $cache = array(); |
| 275 |
$post_id = is_int( $post ) ? $post : $post->ID; |
| 276 |
if ( isset( $cache[ $post_id ] ) ) { |
| 277 |
return $cache[ $post_id ]; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Filters whether the post password check should be skipped when getting the post trackable status. |
| 282 |
* |
| 283 |
* @since 3.0.1 |
| 284 |
* |
| 285 |
* @param bool $skip True if the password check should be skipped. |
| 286 |
* @param int|WP_Post $post Which post object or ID is being checked. |
| 287 |
* |
| 288 |
* @returns bool |
| 289 |
*/ |
| 290 |
$skip_password_check = apply_filters( 'wp_parsely_skip_post_password_check', false, $post ); |
| 291 |
if ( ! $skip_password_check && post_password_required( $post ) ) { |
| 292 |
$cache[ $post_id ] = false; |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Filters the statuses that are permitted to be tracked. |
| 298 |
* |
| 299 |
* By default, the only status tracked is 'publish'. Use this filter if you have other published content that has a different (custom) status. |
| 300 |
* |
| 301 |
* @since 2.5.0 |
| 302 |
* |
| 303 |
* @param string[] $trackable_statuses The list of post statuses that are allowed to be tracked. |
| 304 |
* @param int|WP_Post $post Which post object or ID is being checked. |
| 305 |
*/ |
| 306 |
$statuses = apply_filters( 'wp_parsely_trackable_statuses', array( 'publish' ), $post ); |
| 307 |
$cache[ $post_id ] = in_array( get_post_status( $post ), $statuses, true ); |
| 308 |
return $cache[ $post_id ]; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Creates parsely metadata object from post metadata. |
| 313 |
* |
| 314 |
* @param array $parsely_options parsely_options array. |
| 315 |
* @param WP_Post $post object. |
| 316 |
* @return array |
| 317 |
*/ |
| 318 |
public function construct_parsely_metadata( array $parsely_options, WP_Post $post ): array { |
| 319 |
$parsely_page = array( |
| 320 |
'@context' => 'http://schema.org', |
| 321 |
'@type' => 'WebPage', |
| 322 |
); |
| 323 |
$current_url = $this->get_current_url(); |
| 324 |
$queried_object_id = get_queried_object_id(); |
| 325 |
|
| 326 |
if ( is_front_page() && ! is_paged() ) { |
| 327 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( get_bloginfo( 'name', 'raw' ) ); |
| 328 |
$parsely_page['url'] = home_url(); |
| 329 |
} elseif ( is_front_page() && is_paged() ) { |
| 330 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( get_bloginfo( 'name', 'raw' ) ); |
| 331 |
$parsely_page['url'] = $current_url; |
| 332 |
} elseif ( |
| 333 |
is_home() && ( |
| 334 |
! ( 'page' === get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) ) || |
| 335 |
$queried_object_id && (int) get_option( 'page_for_posts' ) === $queried_object_id |
| 336 |
) |
| 337 |
) { |
| 338 |
$parsely_page['headline'] = get_the_title( get_option( 'page_for_posts', true ) ); |
| 339 |
$parsely_page['url'] = $current_url; |
| 340 |
} elseif ( is_author() ) { |
| 341 |
// TODO: why can't we have something like a WP_User object for all the other cases? Much nicer to deal with than functions. |
| 342 |
$author = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) ); |
| 343 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( 'Author - ' . $author->data->display_name ); |
| 344 |
$parsely_page['url'] = $current_url; |
| 345 |
} elseif ( is_category() || is_post_type_archive() || is_tax() ) { |
| 346 |
$category = get_queried_object(); |
| 347 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( $category->name ); |
| 348 |
$parsely_page['url'] = $current_url; |
| 349 |
} elseif ( is_date() ) { |
| 350 |
if ( is_year() ) { |
| 351 |
/* translators: %s: Archive year */ |
| 352 |
$parsely_page['headline'] = sprintf( __( 'Yearly Archive - %s', 'wp-parsely' ), get_the_time( 'Y' ) ); |
| 353 |
} elseif ( is_month() ) { |
| 354 |
/* translators: %s: Archive month, formatted as F, Y */ |
| 355 |
$parsely_page['headline'] = sprintf( __( 'Monthly Archive - %s', 'wp-parsely' ), get_the_time( 'F, Y' ) ); |
| 356 |
} elseif ( is_day() ) { |
| 357 |
/* translators: %s: Archive day, formatted as F jS, Y */ |
| 358 |
$parsely_page['headline'] = sprintf( __( 'Daily Archive - %s', 'wp-parsely' ), get_the_time( 'F jS, Y' ) ); |
| 359 |
} elseif ( is_time() ) { |
| 360 |
/* translators: %s: Archive time, formatted as F jS g:i:s A */ |
| 361 |
$parsely_page['headline'] = sprintf( __( 'Hourly, Minutely, or Secondly Archive - %s', 'wp-parsely' ), get_the_time( 'F jS g:i:s A' ) ); |
| 362 |
} |
| 363 |
$parsely_page['url'] = $current_url; |
| 364 |
} elseif ( is_tag() ) { |
| 365 |
$tag = single_tag_title( '', false ); |
| 366 |
if ( empty( $tag ) ) { |
| 367 |
$tag = single_term_title( '', false ); |
| 368 |
} |
| 369 |
/* translators: %s: Tag name */ |
| 370 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( sprintf( __( 'Tagged - %s', 'wp-parsely' ), $tag ) ); |
| 371 |
$parsely_page['url'] = $current_url; |
| 372 |
} elseif ( in_array( get_post_type( $post ), $parsely_options['track_post_types'], true ) && self::post_has_trackable_status( $post ) ) { |
| 373 |
$authors = $this->get_author_names( $post ); |
| 374 |
$category = $this->get_category_name( $post, $parsely_options ); |
| 375 |
|
| 376 |
if ( has_post_thumbnail( $post ) ) { |
| 377 |
$image_id = get_post_thumbnail_id( $post ); |
| 378 |
$image_url = wp_get_attachment_image_src( $image_id ); |
| 379 |
$image_url = $image_url[0]; |
| 380 |
} else { |
| 381 |
$image_url = $this->get_first_image( $post ); |
| 382 |
} |
| 383 |
|
| 384 |
$tags = $this->get_tags( $post->ID ); |
| 385 |
if ( $parsely_options['cats_as_tags'] ) { |
| 386 |
$tags = array_merge( $tags, $this->get_categories( $post->ID ) ); |
| 387 |
// add custom taxonomy values. |
| 388 |
$tags = array_merge( $tags, $this->get_custom_taxonomy_values( $post, $parsely_options ) ); |
| 389 |
} |
| 390 |
// the function 'mb_strtolower' is not enabled by default in php, so this check |
| 391 |
// falls back to the native php function 'strtolower' if necessary. |
| 392 |
if ( function_exists( 'mb_strtolower' ) ) { |
| 393 |
$lowercase_callback = 'mb_strtolower'; |
| 394 |
} else { |
| 395 |
$lowercase_callback = 'strtolower'; |
| 396 |
} |
| 397 |
if ( $parsely_options['lowercase_tags'] ) { |
| 398 |
$tags = array_map( $lowercase_callback, $tags ); |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Filters the post tags that are used as metadata keywords. |
| 403 |
* |
| 404 |
* @since 1.8.0 |
| 405 |
* |
| 406 |
* @param string[] $tags Post tags. |
| 407 |
* @param int $ID Post ID. |
| 408 |
*/ |
| 409 |
$tags = apply_filters( 'wp_parsely_post_tags', $tags, $post->ID ); |
| 410 |
$tags = array_map( array( $this, 'get_clean_parsely_page_value' ), $tags ); |
| 411 |
$tags = array_values( array_unique( $tags ) ); |
| 412 |
|
| 413 |
/** |
| 414 |
* Filters the JSON-LD @type. |
| 415 |
* |
| 416 |
* @since 2.5.0 |
| 417 |
* |
| 418 |
* @param array $jsonld_type JSON-LD @type value, default is NewsArticle. |
| 419 |
* @param int $id Post ID. |
| 420 |
* @param string $post_type The Post type in WordPress. |
| 421 |
*/ |
| 422 |
$type = (string) apply_filters( 'wp_parsely_post_type', 'NewsArticle', $post->ID, $post->post_type ); |
| 423 |
$supported_types = array_merge( $this->supported_jsonld_post_types, $this->supported_jsonld_non_post_types ); |
| 424 |
|
| 425 |
// Validate type before passing it further as an invalid type will not be recognized by Parse.ly. |
| 426 |
if ( ! in_array( $type, $supported_types ) ) { |
| 427 |
$error = sprintf( |
| 428 |
/* translators: 1: JSON @type like NewsArticle, 2: URL */ |
| 429 |
__( '@type %1$s is not supported by Parse.ly. Please use a type mentioned in %2$s', 'wp-parsely' ), |
| 430 |
$type, |
| 431 |
'https://www.parse.ly/help/integration/jsonld#distinguishing-between-posts-and-pages' |
| 432 |
); |
| 433 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 434 |
trigger_error( esc_html( $error ), E_USER_WARNING ); |
| 435 |
$type = 'NewsArticle'; |
| 436 |
} |
| 437 |
|
| 438 |
$parsely_page['@type'] = $type; |
| 439 |
$parsely_page['mainEntityOfPage'] = array( |
| 440 |
'@type' => 'WebPage', |
| 441 |
'@id' => $this->get_current_url( 'post' ), |
| 442 |
); |
| 443 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( get_the_title( $post ) ); |
| 444 |
$parsely_page['url'] = $this->get_current_url( 'post', $post->ID ); |
| 445 |
$parsely_page['thumbnailUrl'] = $image_url; |
| 446 |
$parsely_page['image'] = array( |
| 447 |
'@type' => 'ImageObject', |
| 448 |
'url' => $image_url, |
| 449 |
); |
| 450 |
|
| 451 |
$this->set_metadata_post_times( $parsely_page, $post ); |
| 452 |
|
| 453 |
$parsely_page['articleSection'] = $category; |
| 454 |
$author_objects = array(); |
| 455 |
foreach ( $authors as $author ) { |
| 456 |
$author_tag = array( |
| 457 |
'@type' => 'Person', |
| 458 |
'name' => $author, |
| 459 |
); |
| 460 |
$author_objects[] = $author_tag; |
| 461 |
} |
| 462 |
$parsely_page['author'] = $author_objects; |
| 463 |
$parsely_page['creator'] = $authors; |
| 464 |
$parsely_page['publisher'] = array( |
| 465 |
'@type' => 'Organization', |
| 466 |
'name' => get_bloginfo( 'name' ), |
| 467 |
'logo' => $parsely_options['logo'], |
| 468 |
); |
| 469 |
$parsely_page['keywords'] = $tags; |
| 470 |
} elseif ( in_array( get_post_type(), $parsely_options['track_page_types'], true ) && self::post_has_trackable_status( $post ) ) { |
| 471 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( get_the_title( $post ) ); |
| 472 |
$parsely_page['url'] = $this->get_current_url( 'post' ); |
| 473 |
} elseif ( 'page' === get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) ) { |
| 474 |
$parsely_page['headline'] = $this->get_clean_parsely_page_value( get_bloginfo( 'name', 'raw' ) ); |
| 475 |
$parsely_page['url'] = home_url(); |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Filters the structured metadata. |
| 480 |
* |
| 481 |
* @since 2.5.0 |
| 482 |
* |
| 483 |
* @param array $parsely_page Existing structured metadata for a page. |
| 484 |
* @param WP_Post $post Post object. |
| 485 |
* @param array $parsely_options The Parsely options. |
| 486 |
*/ |
| 487 |
return apply_filters( 'wp_parsely_metadata', $parsely_page, $post, $parsely_options ); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Sets all metadata values related to post time. |
| 492 |
* |
| 493 |
* @since 3.0.2 |
| 494 |
* |
| 495 |
* @param array $metadata Array containing all metadata. It will be potentially mutated to add keys: dateCreated, dateModified, & datePublished. |
| 496 |
* @param WP_Post $post Post object from which to extract time data. |
| 497 |
* @return void |
| 498 |
*/ |
| 499 |
private function set_metadata_post_times( array &$metadata, WP_Post $post ): void { |
| 500 |
$date_format = 'Y-m-d\TH:i:s\Z'; |
| 501 |
$post_created_gmt = get_post_time( $date_format, true, $post ); |
| 502 |
|
| 503 |
if ( false === $post_created_gmt ) { |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
$metadata['dateCreated'] = $post_created_gmt; |
| 508 |
$metadata['datePublished'] = $post_created_gmt; |
| 509 |
$metadata['dateModified'] = $post_created_gmt; |
| 510 |
|
| 511 |
$post_modified_gmt = get_post_modified_time( $date_format, true, $post ); |
| 512 |
|
| 513 |
if ( false !== $post_modified_gmt && $post_modified_gmt > $post_created_gmt ) { |
| 514 |
$metadata['dateModified'] = $post_modified_gmt; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Updates the Parsely metadata endpoint with the new metadata of the post. |
| 520 |
* |
| 521 |
* @param int $post_id id of the post to update. |
| 522 |
* @return void |
| 523 |
*/ |
| 524 |
public function update_metadata_endpoint( int $post_id ): void { |
| 525 |
$parsely_options = $this->get_options(); |
| 526 |
|
| 527 |
if ( $this->api_key_is_missing() || empty( $parsely_options['metadata_secret'] ) ) { |
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
$post = get_post( $post_id ); |
| 532 |
$metadata = $this->construct_parsely_metadata( $parsely_options, $post ); |
| 533 |
|
| 534 |
$endpoint_metadata = array( |
| 535 |
'canonical_url' => $metadata['url'], |
| 536 |
'page_type' => $this->convert_jsonld_to_parsely_type( $metadata['@type'] ), |
| 537 |
'title' => $metadata['headline'], |
| 538 |
'image_url' => $metadata['thumbnailUrl'], |
| 539 |
'pub_date_tmsp' => $metadata['datePublished'], |
| 540 |
'section' => $metadata['articleSection'], |
| 541 |
'authors' => $metadata['creator'], |
| 542 |
'tags' => $metadata['keywords'], |
| 543 |
); |
| 544 |
|
| 545 |
$parsely_api_endpoint = 'https://api.parsely.com/v2/metadata/posts'; |
| 546 |
$parsely_metadata_secret = $parsely_options['metadata_secret']; |
| 547 |
$headers = array( |
| 548 |
'Content-Type' => 'application/json', |
| 549 |
); |
| 550 |
$body = wp_json_encode( |
| 551 |
array( |
| 552 |
'secret' => $parsely_metadata_secret, |
| 553 |
'apikey' => $parsely_options['apikey'], |
| 554 |
'metadata' => $endpoint_metadata, |
| 555 |
) |
| 556 |
); |
| 557 |
$response = wp_remote_post( |
| 558 |
$parsely_api_endpoint, |
| 559 |
array( |
| 560 |
'method' => 'POST', |
| 561 |
'headers' => $headers, |
| 562 |
'blocking' => false, |
| 563 |
'body' => $body, |
| 564 |
'data_format' => 'body', |
| 565 |
) |
| 566 |
); |
| 567 |
|
| 568 |
if ( ! is_wp_error( $response ) ) { |
| 569 |
$current_timestamp = time(); |
| 570 |
update_post_meta( $post_id, 'parsely_metadata_last_updated', $current_timestamp ); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* Updates posts with Parsely metadata api in bulk. |
| 576 |
* |
| 577 |
* @return void |
| 578 |
*/ |
| 579 |
public function bulk_update_posts(): void { |
| 580 |
global $wpdb; |
| 581 |
$parsely_options = $this->get_options(); |
| 582 |
$allowed_types = array_merge( $parsely_options['track_post_types'], $parsely_options['track_page_types'] ); |
| 583 |
$allowed_types_string = implode( |
| 584 |
', ', |
| 585 |
array_map( |
| 586 |
function( $v ) { |
| 587 |
return "'" . esc_sql( $v ) . "'"; |
| 588 |
}, |
| 589 |
$allowed_types |
| 590 |
) |
| 591 |
); |
| 592 |
$ids = wp_cache_get( 'parsely_post_ids_need_meta_updating' ); |
| 593 |
if ( false === $ids ) { |
| 594 |
$ids = array(); |
| 595 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 596 |
$results = $wpdb->get_results( |
| 597 |
$wpdb->prepare( "SELECT DISTINCT(id) FROM {$wpdb->posts} WHERE post_type IN (\" . %s . \") AND id NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'parsely_metadata_last_updated');", $allowed_types_string ), |
| 598 |
ARRAY_N |
| 599 |
); |
| 600 |
foreach ( $results as $result ) { |
| 601 |
array_push( $ids, $result[0] ); |
| 602 |
} |
| 603 |
wp_cache_set( 'parsely_post_ids_need_meta_updating', $ids, '', 86400 ); |
| 604 |
} |
| 605 |
|
| 606 |
for ( $i = 0; $i < 100; $i++ ) { |
| 607 |
$post_id = array_pop( $ids ); |
| 608 |
if ( null === $post_id ) { |
| 609 |
wp_clear_scheduled_hook( 'parsely_bulk_metas_update' ); |
| 610 |
break; |
| 611 |
} |
| 612 |
$this->update_metadata_endpoint( $post_id ); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Get the cache buster value for script and styles. |
| 618 |
* |
| 619 |
* If WP_DEBUG is defined and truthy, and we're not running tests, then use a random number. |
| 620 |
* Otherwise, use the plugin version. |
| 621 |
* |
| 622 |
* @since 2.5.0 |
| 623 |
* |
| 624 |
* @return string Random number string or plugin version string. |
| 625 |
*/ |
| 626 |
public static function get_asset_cache_buster(): string { |
| 627 |
static $cache_buster; |
| 628 |
if ( isset( $cache_buster ) ) { |
| 629 |
return $cache_buster; |
| 630 |
} |
| 631 |
|
| 632 |
$cache_buster = defined( 'WP_DEBUG' ) && WP_DEBUG && empty( 'WP_TESTS_DOMAIN' ) ? wp_rand() : PARSELY_VERSION; |
| 633 |
|
| 634 |
/** |
| 635 |
* Filters the cache buster value for linked scripts and styles. |
| 636 |
* |
| 637 |
* @since 2.5.0 |
| 638 |
* |
| 639 |
* @param string $cache_buster Plugin version, unless WP_DEBUG is defined and truthy, and tests are not running. |
| 640 |
*/ |
| 641 |
return apply_filters( 'wp_parsely_cache_buster', (string) $cache_buster ); |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Returns the tags associated with this page or post |
| 646 |
* |
| 647 |
* @param int $post_id The id of the post you're trying to get tags for. |
| 648 |
* @return array The tags of the post represented by the post id. |
| 649 |
*/ |
| 650 |
private function get_tags( int $post_id ): array { |
| 651 |
$tags = array(); |
| 652 |
foreach ( wp_get_post_tags( $post_id ) as $wp_tag ) { |
| 653 |
array_push( $tags, $wp_tag->name ); |
| 654 |
} |
| 655 |
|
| 656 |
return $tags; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Returns an array of all the child categories for the current post |
| 661 |
* |
| 662 |
* @param int $post_id The id of the post you're trying to get categories for. |
| 663 |
* @param string $delimiter What character will delimit the categories. |
| 664 |
* @return array All the child categories of the current post. |
| 665 |
*/ |
| 666 |
private function get_categories( int $post_id, string $delimiter = '/' ): array { |
| 667 |
$tags = array(); |
| 668 |
foreach ( get_the_category( $post_id ) as $category ) { |
| 669 |
$hierarchy = get_category_parents( $category, false, $delimiter ); |
| 670 |
$hierarchy = rtrim( $hierarchy, '/' ); |
| 671 |
array_push( $tags, $hierarchy ); |
| 672 |
} |
| 673 |
// take last element in the hierarchy, a string representing the full parent->child tree, |
| 674 |
// and split it into individual category names. |
| 675 |
$tags = explode( '/', end( $tags ) ); |
| 676 |
// remove uncategorized value from tags. |
| 677 |
return array_diff( $tags, array( 'Uncategorized' ) ); |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Safely returns options for the plugin by assigning defaults contained in optionDefaults. As soon as actual |
| 682 |
* options are saved, they override the defaults. This prevents us from having to do a lot of isset() checking |
| 683 |
* on variables. |
| 684 |
* |
| 685 |
* @return array |
| 686 |
*/ |
| 687 |
public function get_options(): array { |
| 688 |
$options = get_option( self::OPTIONS_KEY, $this->option_defaults ); |
| 689 |
|
| 690 |
if ( ! is_array( $options ) ) { |
| 691 |
return $this->option_defaults; |
| 692 |
} |
| 693 |
|
| 694 |
return array_merge( $this->option_defaults, $options ); |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Returns a properly cleaned category/taxonomy value and will optionally use the top-level category/taxonomy value |
| 699 |
* if so instructed via the `use_top_level_cats` option. |
| 700 |
* |
| 701 |
* @param WP_Post $post_obj The object for the post. |
| 702 |
* @param array $parsely_options The parsely options. |
| 703 |
* @return string Cleaned category name for the post in question. |
| 704 |
*/ |
| 705 |
private function get_category_name( WP_Post $post_obj, array $parsely_options ): string { |
| 706 |
$taxonomy_dropdown_choice = get_the_terms( $post_obj->ID, $parsely_options['custom_taxonomy_section'] ); |
| 707 |
// Get top-level taxonomy name for chosen taxonomy and assign to $parent_name; it will be used |
| 708 |
// as the category value if 'use_top_level_cats' option is checked. |
| 709 |
// Assign as "Uncategorized" if no value is checked for the chosen taxonomy. |
| 710 |
$category = 'Uncategorized'; |
| 711 |
if ( ! empty( $taxonomy_dropdown_choice ) ) { |
| 712 |
if ( $parsely_options['use_top_level_cats'] ) { |
| 713 |
$first_term = array_shift( $taxonomy_dropdown_choice ); |
| 714 |
$term_name = $this->get_top_level_term( $first_term->term_id, $first_term->taxonomy ); |
| 715 |
} else { |
| 716 |
$term_name = $this->get_bottom_level_term( $post_obj->ID, $parsely_options['custom_taxonomy_section'] ); |
| 717 |
} |
| 718 |
|
| 719 |
if ( $term_name ) { |
| 720 |
$category = $term_name; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Filters the constructed category name that are used as metadata keywords. |
| 726 |
* |
| 727 |
* @since 1.8.0 |
| 728 |
* |
| 729 |
* @param string $category Category name. |
| 730 |
* @param WP_Post $post_obj Post object. |
| 731 |
* @param array $parsely_options The Parsely options. |
| 732 |
*/ |
| 733 |
$category = apply_filters( 'wp_parsely_post_category', $category, $post_obj, $parsely_options ); |
| 734 |
|
| 735 |
return $this->get_clean_parsely_page_value( $category ); |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Return the top-most category/taxonomy value in a hierarcy given a taxonomy value's ID |
| 740 |
* ( WordPress calls taxonomy values 'terms' ). |
| 741 |
* |
| 742 |
* @param int $term_id The id of the top level term. |
| 743 |
* @param string $taxonomy_name The name of the taxonomy. |
| 744 |
* @return string|false $parent The top level name of the category / taxonomy. |
| 745 |
*/ |
| 746 |
private function get_top_level_term( int $term_id, string $taxonomy_name ) { |
| 747 |
$parent = get_term_by( 'id', $term_id, $taxonomy_name ); |
| 748 |
while ( false !== $parent && 0 !== $parent->parent ) { |
| 749 |
$parent = get_term_by( 'id', $parent->parent, $taxonomy_name ); |
| 750 |
} |
| 751 |
return $parent ? $parent->name : false; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Return the bottom-most category/taxonomy value in a hierarcy given a post ID |
| 756 |
* ( WordPress calls taxonomy values 'terms' ). |
| 757 |
* |
| 758 |
* @param int $post_id The post id you're interested in. |
| 759 |
* @param string $taxonomy_name The name of the taxonomy. |
| 760 |
* @return string Name of the custom taxonomy. |
| 761 |
*/ |
| 762 |
private function get_bottom_level_term( int $post_id, string $taxonomy_name ): string { |
| 763 |
$terms = get_the_terms( $post_id, $taxonomy_name ); |
| 764 |
$term_ids = is_array( $terms ) ? wp_list_pluck( $terms, 'term_id' ) : array(); |
| 765 |
$parents = is_array( $terms ) ? array_filter( wp_list_pluck( $terms, 'parent' ) ) : array(); |
| 766 |
|
| 767 |
// Get array of IDs of terms which are not parents. |
| 768 |
$term_ids_not_parents = array_diff( $term_ids, $parents ); |
| 769 |
// Get corresponding term objects, which are mapped to array index keys. |
| 770 |
$terms_not_parents = array_intersect_key( $terms, $term_ids_not_parents ); |
| 771 |
// remove array index keys. |
| 772 |
$terms_not_parents_cleaned = array(); |
| 773 |
foreach ( $terms_not_parents as $index => $value ) { |
| 774 |
array_push( $terms_not_parents_cleaned, $value ); |
| 775 |
} |
| 776 |
// if you assign multiple child terms in a custom taxonomy, will only return the first. |
| 777 |
return $terms_not_parents_cleaned[0]->name; |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* Get all term values from custom taxonomies. |
| 782 |
* |
| 783 |
* @param WP_Post $post_obj The post object. |
| 784 |
* @param array $parsely_options Unused? The parsely options. |
| 785 |
* @return array |
| 786 |
*/ |
| 787 |
private function get_custom_taxonomy_values( WP_Post $post_obj, array $parsely_options ): array { |
| 788 |
// filter out default WordPress taxonomies. |
| 789 |
$all_taxonomies = array_diff( get_taxonomies(), array( 'post_tag', 'nav_menu', 'author', 'link_category', 'post_format' ) ); |
| 790 |
$all_values = array(); |
| 791 |
|
| 792 |
if ( is_array( $all_taxonomies ) ) { |
| 793 |
foreach ( $all_taxonomies as $taxonomy ) { |
| 794 |
$custom_taxonomy_objects = get_the_terms( $post_obj->ID, $taxonomy ); |
| 795 |
if ( is_array( $custom_taxonomy_objects ) ) { |
| 796 |
foreach ( $custom_taxonomy_objects as $custom_taxonomy_object ) { |
| 797 |
array_push( $all_values, $custom_taxonomy_object->name ); |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
return $all_values; |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Returns a list of coauthors for a post assuming the Co-Authors Plus plugin is |
| 807 |
* installed. Borrowed from |
| 808 |
* https://github.com/Automattic/Co-Authors-Plus/blob/master/template-tags.php#L3-35 |
| 809 |
* |
| 810 |
* @param int $post_id The id of the post. |
| 811 |
* @return array |
| 812 |
*/ |
| 813 |
private function get_coauthor_names( int $post_id ): array { |
| 814 |
$coauthors = array(); |
| 815 |
if ( class_exists( 'coauthors_plus' ) ) { |
| 816 |
global $post, $post_ID, $coauthors_plus; |
| 817 |
|
| 818 |
if ( ! $post_id && $post_ID ) { |
| 819 |
$post_id = $post_ID; |
| 820 |
} |
| 821 |
|
| 822 |
if ( ! $post_id && $post ) { |
| 823 |
$post_id = $post->ID; |
| 824 |
} |
| 825 |
|
| 826 |
if ( $post_id ) { |
| 827 |
$coauthor_terms = get_the_terms( $post_id, $coauthors_plus->coauthor_taxonomy ); |
| 828 |
|
| 829 |
if ( is_array( $coauthor_terms ) && ! empty( $coauthor_terms ) ) { |
| 830 |
foreach ( $coauthor_terms as $coauthor ) { |
| 831 |
$coauthor_slug = preg_replace( '#^cap-#', '', $coauthor->slug ); |
| 832 |
$post_author = $coauthors_plus->get_coauthor_by( 'user_nicename', $coauthor_slug ); |
| 833 |
// In case the user has been deleted while plugin was deactivated. |
| 834 |
if ( ! empty( $post_author ) ) { |
| 835 |
$coauthors[] = new WP_User( $post_author ); |
| 836 |
} |
| 837 |
} |
| 838 |
} elseif ( ! $coauthors_plus->force_guest_authors ) { |
| 839 |
if ( $post && $post_id === $post->ID ) { |
| 840 |
$post_author = get_userdata( $post->post_author ); |
| 841 |
} |
| 842 |
if ( ! empty( $post_author ) ) { |
| 843 |
$coauthors[] = $post_author; |
| 844 |
} |
| 845 |
} // the empty else case is because if we force guest authors, we don't ever care what value wp_posts.post_author has. |
| 846 |
} |
| 847 |
} |
| 848 |
return $coauthors; |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* Determine author name from display name, falling back to firstname |
| 853 |
* lastname, then nickname and finally the nicename. |
| 854 |
* |
| 855 |
* @param ?WP_User $author The author of the post. |
| 856 |
* @return string |
| 857 |
*/ |
| 858 |
private function get_author_name( ?WP_User $author ): string { |
| 859 |
// gracefully handle situation where no author is available. |
| 860 |
if ( empty( $author ) || ! is_object( $author ) ) { |
| 861 |
return ''; |
| 862 |
} |
| 863 |
|
| 864 |
$author_name = $author->display_name; |
| 865 |
if ( ! empty( $author_name ) ) { |
| 866 |
return $author_name; |
| 867 |
} |
| 868 |
|
| 869 |
$author_name = $author->user_firstname . ' ' . $author->user_lastname; |
| 870 |
if ( ' ' !== $author_name ) { |
| 871 |
return $author_name; |
| 872 |
} |
| 873 |
|
| 874 |
$author_name = $author->nickname; |
| 875 |
if ( ! empty( $author_name ) ) { |
| 876 |
return $author_name; |
| 877 |
} |
| 878 |
|
| 879 |
return $author->user_nicename; |
| 880 |
} |
| 881 |
|
| 882 |
/** |
| 883 |
* Retrieve all the authors for a post as an array. Can include multiple |
| 884 |
* authors if coauthors plugin is in use. |
| 885 |
* |
| 886 |
* @param WP_Post $post The post object. |
| 887 |
* @return array |
| 888 |
*/ |
| 889 |
private function get_author_names( WP_Post $post ): array { |
| 890 |
$authors = $this->get_coauthor_names( $post->ID ); |
| 891 |
if ( empty( $authors ) ) { |
| 892 |
$post_author = get_user_by( 'id', $post->post_author ); |
| 893 |
if ( $post_author ) { |
| 894 |
$authors = array( $post_author ); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
/** |
| 899 |
* Filters the list of author WP_User objects for a post. |
| 900 |
* |
| 901 |
* @since 1.14.0 |
| 902 |
* |
| 903 |
* @param WP_User[] $authors One or more authors as WP_User objects. |
| 904 |
* @param WP_Post $post Post object. |
| 905 |
*/ |
| 906 |
$authors = apply_filters( 'wp_parsely_pre_authors', $authors, $post ); |
| 907 |
|
| 908 |
// Getting the author name for each author. |
| 909 |
$authors = array_map( array( $this, 'get_author_name' ), $authors ); |
| 910 |
|
| 911 |
/** |
| 912 |
* Filters the list of author names for a post. |
| 913 |
* |
| 914 |
* @since 1.14.0 |
| 915 |
* |
| 916 |
* @param string[] $authors One or more author names. |
| 917 |
* @param WP_Post $post Post object. |
| 918 |
*/ |
| 919 |
$authors = apply_filters( 'wp_parsely_post_authors', $authors, $post ); |
| 920 |
$authors = array_map( array( $this, 'get_clean_parsely_page_value' ), $authors ); |
| 921 |
return $authors; |
| 922 |
} |
| 923 |
|
| 924 |
/** |
| 925 |
* Sanitize content |
| 926 |
* |
| 927 |
* @since 2.6.0 |
| 928 |
* |
| 929 |
* @param string $val The content you'd like sanitized. |
| 930 |
* @return string |
| 931 |
*/ |
| 932 |
public function get_clean_parsely_page_value( string $val ): string { |
| 933 |
$val = str_replace( "\n", '', $val ); |
| 934 |
$val = str_replace( "\r", '', $val ); |
| 935 |
$val = wp_strip_all_tags( $val ); |
| 936 |
return trim( $val ); |
| 937 |
} |
| 938 |
|
| 939 |
/** |
| 940 |
* Get the URL of the plugin settings page. |
| 941 |
* |
| 942 |
* @return string |
| 943 |
*/ |
| 944 |
public static function get_settings_url(): string { |
| 945 |
return admin_url( 'options-general.php?page=' . self::MENU_SLUG ); |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Get the URL of the current PHP script. |
| 950 |
* A fall-back implementation to determine permalink |
| 951 |
* |
| 952 |
* @since 3.0.0 $parsely_type Default parameter changed to `non-post`. |
| 953 |
* |
| 954 |
* @param string $parsely_type Optional. Parse.ly post type you're interested in, either 'post' or 'non-post'. Default is 'non-post'. |
| 955 |
* @param int $post_id Optional. ID of the post you want to get the URL for. Default is 0, which means the global `$post` is used. |
| 956 |
* @return string |
| 957 |
*/ |
| 958 |
public function get_current_url( string $parsely_type = 'non-post', int $post_id = 0 ): string { |
| 959 |
if ( 'post' === $parsely_type ) { |
| 960 |
$permalink = (string) get_permalink( $post_id ); |
| 961 |
|
| 962 |
/** |
| 963 |
* Filters the permalink for a post. |
| 964 |
* |
| 965 |
* @since 1.14.0 |
| 966 |
* @since 2.5.0 Added $post_id. |
| 967 |
* |
| 968 |
* @param string $permalink The permalink URL or false if post does not exist. |
| 969 |
* @param string $parsely_type Parse.ly type ("post" or "non-post"). |
| 970 |
* @param int $post_id ID of the post you want to get the URL for. May be 0, so $permalink will be |
| 971 |
* for the global $post. |
| 972 |
*/ |
| 973 |
$url = apply_filters( 'wp_parsely_permalink', $permalink, $parsely_type, $post_id ); |
| 974 |
} else { |
| 975 |
$request_uri = isset( $_SERVER['REQUEST_URI'] ) |
| 976 |
? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) |
| 977 |
: ''; |
| 978 |
|
| 979 |
$url = home_url( $request_uri ); |
| 980 |
} |
| 981 |
|
| 982 |
$options = $this->get_options(); |
| 983 |
return $options['force_https_canonicals'] |
| 984 |
? str_replace( 'http://', 'https://', $url ) |
| 985 |
: str_replace( 'https://', 'http://', $url ); |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* Get the first image from a post |
| 990 |
* https://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/ |
| 991 |
* |
| 992 |
* @param WP_Post $post The post object you're interested in. |
| 993 |
* @return string |
| 994 |
*/ |
| 995 |
public function get_first_image( WP_Post $post ): string { |
| 996 |
ob_start(); |
| 997 |
ob_end_clean(); |
| 998 |
if ( preg_match_all( '/<img.+src=[\'"]( [^\'"]+ )[\'"].*>/i', $post->post_content, $matches ) ) { |
| 999 |
return $matches[1][0]; |
| 1000 |
} |
| 1001 |
return ''; |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** |
| 1005 |
* Check to see if parsely user is logged in. |
| 1006 |
* |
| 1007 |
* @return bool |
| 1008 |
*/ |
| 1009 |
public function parsely_is_user_logged_in(): bool { |
| 1010 |
// can't use $blog_id here because it futzes with the global $blog_id. |
| 1011 |
$current_blog_id = get_current_blog_id(); |
| 1012 |
$current_user_id = get_current_user_id(); |
| 1013 |
return is_user_member_of_blog( $current_user_id, $current_blog_id ); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** |
| 1017 |
* Convert JSON-LD type to respective Parse.ly page type. |
| 1018 |
* |
| 1019 |
* If the JSON-LD type is one of the types Parse.ly supports as a "post", then "post" will be returned. |
| 1020 |
* Otherwise, for "non-posts" and unknown types, "index" is returned. |
| 1021 |
* |
| 1022 |
* @since 2.5.0 |
| 1023 |
* |
| 1024 |
* @see https://www.parse.ly/help/integration/metatags#field-description |
| 1025 |
* |
| 1026 |
* @param string $type JSON-LD type. |
| 1027 |
* @return string "post" or "index". |
| 1028 |
*/ |
| 1029 |
public function convert_jsonld_to_parsely_type( string $type ): string { |
| 1030 |
return in_array( $type, $this->supported_jsonld_post_types ) ? 'post' : 'index'; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* Determine if an API key is saved in the options. |
| 1035 |
* |
| 1036 |
* @since 2.6.0 |
| 1037 |
* |
| 1038 |
* @return bool True is API key is set, false if it is missing. |
| 1039 |
*/ |
| 1040 |
public function api_key_is_set(): bool { |
| 1041 |
$options = $this->get_options(); |
| 1042 |
|
| 1043 |
return ( |
| 1044 |
isset( $options['apikey'] ) && |
| 1045 |
is_string( $options['apikey'] ) && |
| 1046 |
'' !== $options['apikey'] |
| 1047 |
); |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* Determine if an API key is not saved in the options. |
| 1052 |
* |
| 1053 |
* @since 2.6.0 |
| 1054 |
* |
| 1055 |
* @return bool True if API key is missing, false if it is set. |
| 1056 |
*/ |
| 1057 |
public function api_key_is_missing(): bool { |
| 1058 |
return ! $this->api_key_is_set(); |
| 1059 |
} |
| 1060 |
|
| 1061 |
/** |
| 1062 |
* Get the API key if set. |
| 1063 |
* |
| 1064 |
* @since 2.6.0 |
| 1065 |
* |
| 1066 |
* @return string API key if set, or empty string if not. |
| 1067 |
*/ |
| 1068 |
public function get_api_key(): string { |
| 1069 |
$options = $this->get_options(); |
| 1070 |
|
| 1071 |
return $this->api_key_is_set() ? $options['apikey'] : ''; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|