| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Third_Party; |
| 4 |
|
| 5 |
use WP_Post; |
| 6 |
use Yoast\WP\SEO\Actions\Wincher\Wincher_Account_Action; |
| 7 |
use Yoast\WP\SEO\Actions\Wincher\Wincher_Keyphrases_Action; |
| 8 |
use Yoast\WP\SEO\Conditionals\Wincher_Automatically_Track_Conditional; |
| 9 |
use Yoast\WP\SEO\Conditionals\Wincher_Conditional; |
| 10 |
use Yoast\WP\SEO\Conditionals\Wincher_Enabled_Conditional; |
| 11 |
use Yoast\WP\SEO\Conditionals\Wincher_Token_Conditional; |
| 12 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 13 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles automatically tracking published posts with Wincher. |
| 17 |
*/ |
| 18 |
class Wincher_Publish implements Integration_Interface { |
| 19 |
|
| 20 |
/** |
| 21 |
* The Wincher enabled conditional. |
| 22 |
* |
| 23 |
* @var Wincher_Enabled_Conditional |
| 24 |
*/ |
| 25 |
protected $wincher_enabled; |
| 26 |
|
| 27 |
/** |
| 28 |
* The options helper. |
| 29 |
* |
| 30 |
* @var Options_Helper |
| 31 |
*/ |
| 32 |
protected $options_helper; |
| 33 |
|
| 34 |
/** |
| 35 |
* The Wincher keyphrases action handler. |
| 36 |
* |
| 37 |
* @var Wincher_Keyphrases_Action |
| 38 |
*/ |
| 39 |
protected $keyphrases_action; |
| 40 |
|
| 41 |
/** |
| 42 |
* The Wincher account action handler. |
| 43 |
* |
| 44 |
* @var Wincher_Account_Action |
| 45 |
*/ |
| 46 |
protected $account_action; |
| 47 |
|
| 48 |
/** |
| 49 |
* Wincher publish constructor. |
| 50 |
* |
| 51 |
* @param Wincher_Enabled_Conditional $wincher_enabled The WPML WPSEO conditional. |
| 52 |
* @param Options_Helper $options_helper The options helper. |
| 53 |
* @param Wincher_Keyphrases_Action $keyphrases_action The keyphrases action class. |
| 54 |
* @param Wincher_Account_Action $account_action The account action class. |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
Wincher_Enabled_Conditional $wincher_enabled, |
| 58 |
Options_Helper $options_helper, |
| 59 |
Wincher_Keyphrases_Action $keyphrases_action, |
| 60 |
Wincher_Account_Action $account_action |
| 61 |
) { |
| 62 |
$this->wincher_enabled = $wincher_enabled; |
| 63 |
$this->options_helper = $options_helper; |
| 64 |
$this->keyphrases_action = $keyphrases_action; |
| 65 |
$this->account_action = $account_action; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Initializes the integration. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function register_hooks() { |
| 74 |
/** |
| 75 |
* Called in the REST API when submitting the post copy in the Block Editor. |
| 76 |
* Runs the republishing of the copy onto the original. |
| 77 |
*/ |
| 78 |
\add_action( 'rest_after_insert_post', [ $this, 'track_after_rest_api_request' ] ); |
| 79 |
|
| 80 |
/** |
| 81 |
* Called by `wp_insert_post()` when submitting the post copy, which runs in two cases: |
| 82 |
* - In the Classic Editor, where there's only one request that updates everything. |
| 83 |
* - In the Block Editor, only when there are custom meta boxes. |
| 84 |
*/ |
| 85 |
\add_action( 'wp_insert_post', [ $this, 'track_after_post_request' ], \PHP_INT_MAX, 2 ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the conditionals based in which this loadable should be active. |
| 90 |
* |
| 91 |
* This integration should only be active when the feature is enabled, a token is available and automatically tracking is enabled. |
| 92 |
* |
| 93 |
* @return array The conditionals. |
| 94 |
*/ |
| 95 |
public static function get_conditionals() { |
| 96 |
return [ |
| 97 |
Wincher_Conditional::class, |
| 98 |
Wincher_Enabled_Conditional::class, |
| 99 |
Wincher_Automatically_Track_Conditional::class, |
| 100 |
Wincher_Token_Conditional::class, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Determines whether the current request is a REST request. |
| 106 |
* |
| 107 |
* @return bool Whether the request is a REST request. |
| 108 |
*/ |
| 109 |
public function is_rest_request() { |
| 110 |
return \defined( 'REST_REQUEST' ) && \REST_REQUEST; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Sends the keyphrases associated with the post to Wincher for automatic tracking. |
| 115 |
* |
| 116 |
* @param WP_Post $post The post to extract the keyphrases from. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function track_request( $post ) { |
| 121 |
if ( ! $post instanceof \WP_Post ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// Filter out empty entries. |
| 126 |
$keyphrases = \array_filter( $this->keyphrases_action->collect_keyphrases_from_post( $post ) ); |
| 127 |
|
| 128 |
if ( ! empty( $keyphrases ) ) { |
| 129 |
$this->keyphrases_action->track_keyphrases( $keyphrases, $this->account_action->check_limit() ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Republishes the original post with the passed post, when using the Block Editor. |
| 135 |
* |
| 136 |
* @param WP_Post $post The copy's post object. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function track_after_rest_api_request( $post ) { |
| 141 |
$this->track_request( $post ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Republishes the original post with the passed post, when using the Classic Editor. |
| 146 |
* |
| 147 |
* Runs also in the Block Editor to save the custom meta data only when there |
| 148 |
* are custom meta boxes. |
| 149 |
* |
| 150 |
* @param int $post_id The copy's post ID. |
| 151 |
* @param WP_Post $post The copy's post object. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public function track_after_post_request( $post_id, $post ) { |
| 156 |
if ( $this->is_rest_request() ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$this->track_request( $post ); |
| 161 |
} |
| 162 |
} |
| 163 |
|