PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / third-party / wincher-publish.php

wincher-publish.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.0, at src/integrations/third-party/wincher-publish.php

167 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @deprecated 23.6
108 * @codeCoverageIgnore
109 *
110 * @return bool Whether the request is a REST request.
111 */
112 public function is_rest_request() {
113 \_deprecated_function( __METHOD__, 'Yoast SEO 23.6', 'wp_is_serving_rest_request' );
114 return \defined( 'REST_REQUEST' ) && \REST_REQUEST;
115 }
116
117 /**
118 * Sends the keyphrases associated with the post to Wincher for automatic tracking.
119 *
120 * @param WP_Post $post The post to extract the keyphrases from.
121 *
122 * @return void
123 */
124 public function track_request( $post ) {
125 if ( ! $post instanceof WP_Post ) {
126 return;
127 }
128
129 // Filter out empty entries.
130 $keyphrases = \array_filter( $this->keyphrases_action->collect_keyphrases_from_post( $post ) );
131
132 if ( ! empty( $keyphrases ) ) {
133 $this->keyphrases_action->track_keyphrases( $keyphrases, $this->account_action->check_limit() );
134 }
135 }
136
137 /**
138 * Republishes the original post with the passed post, when using the Block Editor.
139 *
140 * @param WP_Post $post The copy's post object.
141 *
142 * @return void
143 */
144 public function track_after_rest_api_request( $post ) {
145 $this->track_request( $post );
146 }
147
148 /**
149 * Republishes the original post with the passed post, when using the Classic Editor.
150 *
151 * Runs also in the Block Editor to save the custom meta data only when there
152 * are custom meta boxes.
153 *
154 * @param int $post_id The copy's post ID.
155 * @param WP_Post $post The copy's post object.
156 *
157 * @return void
158 */
159 public function track_after_post_request( $post_id, $post ) {
160 if ( \wp_is_serving_rest_request() ) {
161 return;
162 }
163
164 $this->track_request( $post );
165 }
166 }
167