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