diff --git a/src/class-trigger-handler.php b/src/class-trigger-handler.php index 2be09ca..66509c5 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,47 @@ 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' ); + + $options = $request->get_param( 'options' ); + + // Post id is required and must be int. + if ( ! is_int( $id ) ) { + $this->error( new Invalid_Payload_Exception() ); + + return; + } + + if ( empty( $options ) || ! is_array( $options ) ) { + $this->error( new Invalid_Payload_Exception() ); + + return; + } + + // Ensure the value is of the correct type. + if ( isset( $options['seo_title'] ) && ! is_string( $options['seo_title'] ) + || isset( $options['seo_description'] ) && ! is_string( $options['seo_description'] ) + || isset( $options['og_title'] ) && ! is_string( $options['og_title'] ) + || isset( $options['og_description'] ) && ! is_string( $options['og_description'] ) + || isset( $options['og_image_id'] ) && ! is_int( $options['og_image_id'] ) + ) { + $this->error( new Invalid_Payload_Exception() ); + + return; + } + + $this->handle( new Update_Yoast_Seo_Metadata( $id, $options ) ); + } + /** * 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 @@ +post_id = $post_id; + + $this->options = $options; + } + + /** + * {@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 ( isset( $this->options['seo_title'] ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_title', $this->options['seo_title'] ); + } + + if ( isset( $this->options['seo_description'] ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_metadesc', $this->options['seo_description'] ); + } + + if ( isset( $this->options['og_title'] ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-title', $this->options['og_title'] ); + } + + if ( isset( $this->options['og_description'] ) ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-description', $this->options['og_description'] ); + } + + if ( isset( $this->options['og_image_id'] ) ) { + if ( -1 === $this->options['og_image_id'] ) { + delete_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-image-id' ); + + delete_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-image' ); + } else { + $post = get_post( $this->options['og_image_id'] ); + + if ( $post instanceof WP_Post && 'attachment' === $post->post_type ) { + update_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-image-id', (string) $this->options['og_image_id'] ); + + update_metadata( 'post', $this->post_id, '_yoast_wpseo_opengraph-image', $post->guid ); + } + } + } + + return array(); + } +}