Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update yoast seo metadata api #10

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/class-trigger-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -117,6 +124,47 @@ public function acf_data() {
$this->handle( new ACF_Data() );
}

/**
* Update Yoast seo metadata
*
* @param WP_REST_Request<array{}> $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.
*
Expand Down
24 changes: 24 additions & 0 deletions src/errors/class-post-not-found-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Storipress
*
* @package Storipress
*/

declare(strict_types=1);

namespace Storipress\Storipress\Errors;

/**
* Invalid request payload exception.
*
* @since 0.0.14
*/
final class Post_Not_Found_Exception extends Exception {
/**
* Constructor.
*/
public function __construct() {
parent::__construct( '', 4041001 );
}
}
134 changes: 134 additions & 0 deletions src/triggers/class-update-yoast-seo-metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Storipress
*
* @package Storipress
*/

declare(strict_types=1);

namespace Storipress\Storipress\Triggers;

use Storipress;
use WP_Post;

/**
* The acf data trigger.
*
* @since 0.0.14
*/
final class Update_Yoast_Seo_Metadata extends Trigger {

/**
* Plugin file.
*
* @var string
*/
public $file = 'wordpress-seo/wp-seo.php';

/**
* The post id.
*
* @var int
*/
public $post_id;

/**
* The seo title.
*
* @var array{
* seo_title?: string,
* seo_description?: string,
* og_title?: string,
* og_description?: string,
* og_image_id?: int
* }
*/
public $options;

/**
* The seo description.
*
* @var string|null
*/
public $description;

/**
* Constructor.
*
* @param int $post_id The post id.
* @param array{seo_title?: string, seo_description?: string, og_title?: string, og_description?: string, og_image_id?: int } $options The seo options.
*/
public function __construct( int $post_id, array $options ) {
$this->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();
}
}