PluginProbe
Parse.ly / 3.17.0
Parse.ly v3.17.0
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
wp-parsely / src / class-validator.php

class-validator.php in Parse.ly 3.17.0, at src/class-validator.php

70 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Validator class
4 *
5 * @package Parsely
6 * @since 3.9.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely;
12
13 use WP_Error;
14
15 /**
16 * Contains a variety of validation functions.
17 *
18 * @since 3.9.0
19 */
20 class Validator {
21
22 public const INVALID_API_CREDENTIALS = 'invalid_api_credentials';
23
24 /**
25 * Validates the passed Metadata Secret.
26 *
27 * Currently, the Metadata Secret is considered valid if it is exactly 10
28 * characters.
29 *
30 * @since 3.9.0
31 *
32 * @param string $metadata_secret The Metadata Secret to be validated.
33 * @return bool True if the Metadata Secret is valid, false otherwise.
34 */
35 public static function validate_metadata_secret( string $metadata_secret ): bool {
36 return strlen( $metadata_secret ) === 10;
37 }
38
39 /**
40 * Validates the passed API Credentials.
41 *
42 * @since 3.11.0
43 *
44 * @param Parsely $parsely The Parsely instance.
45 * @param string $site_id The Site ID to be validated.
46 * @param string $api_secret The API Secret to be validated.
47 * @return bool|WP_Error True if the API Credentials are valid, WP_Error otherwise.
48 */
49 public static function validate_api_credentials( Parsely $parsely, string $site_id, string $api_secret ) {
50 // If the API secret is empty, the validation endpoint will always fail.
51 // Since it's possible to use the plugin without an API Secret, we'll
52 // skip the validation and assume it's valid.
53 if ( '' === $api_secret ) {
54 return true;
55 }
56
57 $content_api = $parsely->get_content_api();
58 $is_valid = $content_api->validate_credentials( $site_id, $api_secret );
59
60 if ( is_wp_error( $is_valid ) ) {
61 return new WP_Error(
62 self::INVALID_API_CREDENTIALS,
63 __( 'Invalid API Credentials', 'wp-parsely' )
64 );
65 }
66
67 return $is_valid;
68 }
69 }
70