| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Third_Party; |
| 4 |
|
| 5 |
use WordProof\SDK\Helpers\PostMetaHelper; |
| 6 |
use WordProof\SDK\WordPressSDK; |
| 7 |
use Yoast\WP\SEO\Conditionals\Non_Multisite_Conditional; |
| 8 |
use Yoast\WP\SEO\Conditionals\Third_Party\Wordproof_Plugin_Inactive_Conditional; |
| 9 |
use Yoast\WP\SEO\Config\Wordproof_App_Config; |
| 10 |
use Yoast\WP\SEO\Config\Wordproof_Translations; |
| 11 |
use Yoast\WP\SEO\Helpers\Wordproof_Helper; |
| 12 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WordProof |
| 16 |
* |
| 17 |
* @package Yoast\WP\SEO\Integrations\Third_Party |
| 18 |
*/ |
| 19 |
class Wordproof implements Integration_Interface { |
| 20 |
|
| 21 |
/** |
| 22 |
* The Yoast meta key used to save if a post shiould be timestamped. |
| 23 |
* |
| 24 |
* @var string The Yoast meta key used to save if a post should be timestamped. |
| 25 |
*/ |
| 26 |
protected $post_meta_key = '_yoast_wpseo_wordproof_timestamp'; |
| 27 |
|
| 28 |
/** |
| 29 |
* The WordProof helper instance. |
| 30 |
* |
| 31 |
* @var Wordproof_Helper $wordproof The helper instance. |
| 32 |
*/ |
| 33 |
protected $wordproof; |
| 34 |
|
| 35 |
/** |
| 36 |
* The WordProof integration constructor. |
| 37 |
* |
| 38 |
* @param Wordproof_Helper $wordproof The WordProof helper instance. |
| 39 |
*/ |
| 40 |
public function __construct( Wordproof_Helper $wordproof ) { |
| 41 |
$this->wordproof = $wordproof; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the conditionals based in which this loadable should be active. |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public static function get_conditionals() { |
| 50 |
return [ Wordproof_Plugin_Inactive_Conditional::class, Non_Multisite_Conditional::class ]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Initializes the integration. |
| 55 |
* |
| 56 |
* This is the place to register hooks and filters. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function register_hooks() { |
| 61 |
/** |
| 62 |
* Used to initialize the WordProof WordPress SDK. |
| 63 |
*/ |
| 64 |
\add_action( 'init', [ $this, 'sdk_setup' ], 11 ); |
| 65 |
|
| 66 |
/** |
| 67 |
* Removes the post meta timestamp key for the old privacy page. |
| 68 |
*/ |
| 69 |
\add_action( 'update_option_wp_page_for_privacy_policy', [ $this, 'disable_timestamp_for_previous_legal_page' ], 10, 2 ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Removes the post meta timestamp key for the old terms and conditions page. |
| 73 |
*/ |
| 74 |
\add_action( 'update_option_woocommerce_terms_page_id', [ $this, 'disable_timestamp_for_previous_legal_page' ], 10, 2 ); |
| 75 |
|
| 76 |
/** |
| 77 |
* Called by the WordProof WordPress SDK to determine if the post should be timestamped. |
| 78 |
*/ |
| 79 |
\add_filter( 'wordproof_timestamp_post_meta_key_overrides', [ $this, 'add_post_meta_key' ] ); |
| 80 |
|
| 81 |
/** |
| 82 |
* Called by the WordProof WordPress SDK to determine if the certficate should be shown. |
| 83 |
*/ |
| 84 |
\add_filter( 'wordproof_timestamp_show_certificate', [ $this, 'show_certificate' ], 10, 2 ); |
| 85 |
|
| 86 |
/** |
| 87 |
* Called by WPSEO_Meta to add extra meta fields to the ones defined there. |
| 88 |
*/ |
| 89 |
\add_filter( 'add_extra_wpseo_meta_fields', [ $this, 'add_meta_field' ] ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Initializes the WordProof WordPress SDK. |
| 94 |
*/ |
| 95 |
public function sdk_setup() { |
| 96 |
|
| 97 |
$config = new Wordproof_App_Config(); |
| 98 |
$translations = new Wordproof_Translations(); |
| 99 |
|
| 100 |
WordPressSDK::getInstance( $config, $translations ) |
| 101 |
->certificate() |
| 102 |
->initialize(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Removes the WordProof timestamp post meta if a legal page is changed. |
| 107 |
* |
| 108 |
* @param integer $old_post_id The old post id. |
| 109 |
* @param integer $new_post_id The new post id. |
| 110 |
*/ |
| 111 |
public function disable_timestamp_for_previous_legal_page( $old_post_id, $new_post_id ) { |
| 112 |
|
| 113 |
if ( $old_post_id !== $new_post_id ) { |
| 114 |
delete_post_meta( $old_post_id, '_yoast_wpseo_wordproof_timestamp' ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add the Yoast post meta key for the included WordProof SDK to determine if the post should be timestamped. |
| 120 |
* |
| 121 |
* @param array $array The array containing meta keys that should be used. |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function add_post_meta_key( $array ) { |
| 125 |
$array[] = $this->post_meta_key; |
| 126 |
return $array; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This filters hides the certificate if the Yoast post meta key is not set to true. |
| 131 |
* |
| 132 |
* @param bool $value If the certificate should be shown. |
| 133 |
* @param WP_Post $post The post object of the post for which to determine the certificate should be shown. |
| 134 |
* @return bool|null |
| 135 |
*/ |
| 136 |
public function show_certificate( $value, $post ) { |
| 137 |
if ( ! $value ) { |
| 138 |
return $value; |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! $this->wordproof->integration_is_active() ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return boolval( PostMetaHelper::get( $post->ID, $this->post_meta_key ) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Adds the WordProof integration toggle to the array. |
| 150 |
* |
| 151 |
* @param array $fields The currently registered meta fields. |
| 152 |
* |
| 153 |
* @return array A new array with meta fields. |
| 154 |
*/ |
| 155 |
public function add_meta_field( $fields ) { |
| 156 |
$fields['advanced']['wordproof_timestamp'] = [ |
| 157 |
'type' => 'hidden', |
| 158 |
'title' => '', |
| 159 |
'default_value' => '', |
| 160 |
'description' => '0', |
| 161 |
]; |
| 162 |
|
| 163 |
return $fields; |
| 164 |
} |
| 165 |
} |
| 166 |
|