diff --git a/src/class-trigger-handler.php b/src/class-trigger-handler.php index 2be09ca..cc7bd9b 100644 --- a/src/class-trigger-handler.php +++ b/src/class-trigger-handler.php @@ -12,11 +12,14 @@ use Storipress\Storipress\Errors\Exception; use Storipress\Storipress\Errors\Internal_Error_Exception; use Storipress\Storipress\Errors\Invalid_Payload_Exception; +use Storipress\Storipress\Errors\Post_Not_Found_Exception; use Storipress\Storipress\Triggers\ACF_Data; use Storipress\Storipress\Triggers\Connect; use Storipress\Storipress\Triggers\Disconnect; use Storipress\Storipress\Triggers\Trigger; +use Storipress\Storipress\Triggers\Update_Yoast_Seo_Metadata; use Throwable; +use WP_Post; use WP_REST_Request; use WP_REST_Server; @@ -58,6 +61,10 @@ public function register_routes() { 'path' => '/acf-data', 'callback' => 'acf_data', ), + array( + 'path' => '/update-yoast-seo-metadata', + 'callback' => 'update_yoast_seo_metadata', + ), ); foreach ( $routes as $route ) { @@ -117,6 +124,36 @@ public function acf_data() { $this->handle( new ACF_Data() ); } + /** + * Update Yoast seo metadata + * + * @param WP_REST_Request $request The request instance. + * @return void + * + * @since 0.0.14 + */ + public function update_yoast_seo_metadata( $request ) { + $id = $request->get_param( 'id' ); + + $title = $request->get_param( 'title' ); + + $description = $request->get_param( 'description' ); + + if ( ! is_int( $id ) || ( empty( $title ) && empty( $description ) ) ) { + $this->error( new Invalid_Payload_Exception() ); + + return; + } + + if ( ! ( get_post( $id ) instanceof WP_Post ) ) { + $this->error( new Post_Not_Found_Exception() ); + + return; + } + + $this->handle( new Update_Yoast_Seo_Metadata( $id, $title, $description ) ); + } + /** * Trigger handler. * diff --git a/src/errors/class-post-not-found-exception.php b/src/errors/class-post-not-found-exception.php new file mode 100644 index 0000000..23a1a1a --- /dev/null +++ b/src/errors/class-post-not-found-exception.php @@ -0,0 +1,24 @@ +title = $title; + + $this->description = $description; + + $this->post_id = $post_id; + } + + /** + * {@inheritDoc} + */ + public function validate(): bool { + if ( ! Storipress::instance()->core->is_connected() ) { + return false; + } + + // Needs to include the plugin function on a non-admin page. + if ( ! function_exists( 'get_plugins' ) ) { + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + + // Ensure yoast seo is installed. + if ( ! in_array( $this->file, array_keys( get_plugins() ), true ) ) { + return false; + } + + // Ensure yoast seo is active. + if ( ! is_plugin_active( $this->file ) ) { + return false; + } + + return true; + } + + /** + * {@inheritDoc} + * + * @return array + */ + public function run(): array { + if ( ! empty( $this->title ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_title', $this->title ); + } + + if ( ! empty( $this->description ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_metadesc', $this->description ); + } + + return array(); + } +}