| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update email report settings ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Settings |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Settings; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Email_Report_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Switches ThinkRank Email Reporting on or off. |
| 21 |
* |
| 22 |
* Mirrors `POST /thinkrank/v1/email-report/config`. Returns the resolved config |
| 23 |
* after the write and the next scheduled run. |
| 24 |
*/ |
| 25 |
class Update_Email_Report_Settings extends Ability_Base { |
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->id = 'thinkrank/update-email-report-settings'; |
| 31 |
$this->label = __( 'Update ThinkRank Email Report Settings', 'thinkrank' ); |
| 32 |
$this->description = __( 'Switch the scheduled ThinkRank SEO email report on or off. Switching it on schedules the first report one period from now. Returns the stored config and the next scheduled send. Read the current state with get-email-report-settings.', 'thinkrank' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* {@inheritDoc} |
| 37 |
* |
| 38 |
* @return array<string, bool|float|string> |
| 39 |
*/ |
| 40 |
public function get_annotations() { |
| 41 |
return [ |
| 42 |
'readonly' => false, |
| 43 |
'destructive' => false, |
| 44 |
'idempotent' => true, |
| 45 |
'priority' => 2.0, |
| 46 |
'openWorldHint' => false, |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* {@inheritDoc} |
| 52 |
* |
| 53 |
* @return array<string, mixed> |
| 54 |
*/ |
| 55 |
public function get_input_schema() { |
| 56 |
return [ |
| 57 |
'type' => 'object', |
| 58 |
'additionalProperties' => false, |
| 59 |
'properties' => [ |
| 60 |
'enabled' => [ |
| 61 |
'type' => 'boolean', |
| 62 |
'description' => __( 'Whether the scheduled report is sent.', 'thinkrank' ), |
| 63 |
], |
| 64 |
], |
| 65 |
'required' => [ 'enabled' ], |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritDoc} |
| 71 |
* |
| 72 |
* @return array<string, mixed> |
| 73 |
*/ |
| 74 |
public function get_output_schema() { |
| 75 |
return [ |
| 76 |
'type' => 'object', |
| 77 |
'properties' => [ |
| 78 |
'success' => [ 'type' => 'boolean' ], |
| 79 |
'message' => [ 'type' => 'string' ], |
| 80 |
'config' => [ |
| 81 |
'type' => 'object', |
| 82 |
'description' => __( 'The resolved config after the write.', 'thinkrank' ), |
| 83 |
'additionalProperties' => true, |
| 84 |
], |
| 85 |
'next_run' => [ 'type' => [ 'string', 'null' ] ], |
| 86 |
], |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Execute ability. |
| 92 |
* |
| 93 |
* @param array<string, mixed> $input Ability input payload. |
| 94 |
* @return array<string, mixed>|\WP_Error |
| 95 |
*/ |
| 96 |
public function execute( $input ) { |
| 97 |
if ( ! is_array( $input ) || ! array_key_exists( 'enabled', $input ) ) { |
| 98 |
return new \WP_Error( |
| 99 |
'thinkrank_missing_email_report_settings_payload', |
| 100 |
__( 'The enabled flag is required.', 'thinkrank' ), |
| 101 |
[ 'status' => 400 ] |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
$manager = $this->resolve_manager(); |
| 106 |
|
| 107 |
if ( ! $manager instanceof Email_Report_Manager ) { |
| 108 |
return new \WP_Error( |
| 109 |
'thinkrank_email_report_unavailable', |
| 110 |
__( 'Email Report Manager is unavailable.', 'thinkrank' ), |
| 111 |
[ 'status' => 500 ] |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
$saved = $manager->config()->save( [ 'enabled' => (bool) $input['enabled'] ] ); |
| 116 |
|
| 117 |
return [ |
| 118 |
'success' => true, |
| 119 |
'message' => $saved['enabled'] |
| 120 |
? __( 'Email reporting is on.', 'thinkrank' ) |
| 121 |
: __( 'Email reporting is off.', 'thinkrank' ), |
| 122 |
'config' => $saved, |
| 123 |
'next_run' => $manager->scheduler()->next_run_iso(), |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Resolve the shared Email_Report_Manager from the plugin container. |
| 129 |
* |
| 130 |
* @return Email_Report_Manager|null |
| 131 |
*/ |
| 132 |
private function resolve_manager() { |
| 133 |
if ( ! function_exists( 'thinkrank' ) ) { |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
$component = thinkrank()->get_component( 'email_report' ); |
| 138 |
|
| 139 |
return $component instanceof Email_Report_Manager ? $component : null; |
| 140 |
} |
| 141 |
} |
| 142 |
|