Skip to content

Commit

Permalink
feat: update yoast seo metadata api
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrisbee committed Dec 29, 2023
1 parent 76f6dad commit 9dc0f1b
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
37 changes: 37 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,36 @@ 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' );

$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 ) );

Check failure on line 154 in src/class-trigger-handler.php

View workflow job for this annotation

GitHub Actions / Static Analyze

Parameter #2 $title of class Storipress\Storipress\Triggers\Update_Yoast_Seo_Metadata constructor expects string|null, mixed given.

Check failure on line 154 in src/class-trigger-handler.php

View workflow job for this annotation

GitHub Actions / Static Analyze

Parameter #3 $description of class Storipress\Storipress\Triggers\Update_Yoast_Seo_Metadata constructor expects string|null, mixed given.
}

/**
* 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 );
}
}
106 changes: 106 additions & 0 deletions src/triggers/class-update-yoast-seo-metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Storipress
*
* @package Storipress
*/

declare(strict_types=1);

namespace Storipress\Storipress\Triggers;

use Storipress;

/**
* 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 string|null
*/
public $title;

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

/**
* Constructor.
*
* @param int $post_id The post id.
* @param string|null $title The seo title.
* @param string|null $description The seo description.
*/
public function __construct( int $post_id, string $title = null, string $description = null ) {
$this->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 {

Check failure on line 95 in src/triggers/class-update-yoast-seo-metadata.php

View workflow job for this annotation

GitHub Actions / Static Analyze

Method Storipress\Storipress\Triggers\Update_Yoast_Seo_Metadata::run() return type has no value type specified in iterable type 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();
}
}

0 comments on commit 9dc0f1b

Please sign in to comment.