← All changes
|
includes/abilities/settings/class-update-email-report-settings.php
+16
-41
2.1.1
→
2.8.0
View file →
| @@ -16,15 +16,12 @@ | ||
| 16 | 16 | exit; // Exit if accessed directly. |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Updates ThinkRank Email Reporting settings. | |
| 20 | + * Switches ThinkRank Email Reporting on or off. | |
| 21 | 21 | * |
| 22 | - * Mirrors `POST /thinkrank/v1/email-report/config`: accepts a partial config | |
| 23 | - * object and persists it through Email_Report_Config, which sanitizes and | |
| 24 | - * capability-clamps every field (Pro-only fields submitted on a free plan are | |
| 25 | - * silently coerced to defaults rather than rejected). Returns the persisted | |
| 26 | - * config and the next scheduled run. | |
| 22 | + * Mirrors `POST /thinkrank/v1/email-report/config`. Returns the resolved config | |
| 23 | + * after the write and the next scheduled run. | |
| 27 | 24 | */ |
| 28 | 25 | class Update_Email_Report_Settings extends Ability_Base { |
| 29 | 26 | /** |
| 30 | 27 | * Constructor. |
| @@ -31,9 +28,9 @@ | ||
| 31 | 28 | */ |
| 32 | 29 | public function __construct() { |
| 33 | 30 | $this->id = 'thinkrank/update-email-report-settings'; |
| 34 | 31 | $this->label = __( 'Update ThinkRank Email Report Settings', 'thinkrank' ); |
| 35 | - $this->description = __( 'Update ThinkRank Email Reporting settings: enable toggle, frequency (days), recipients, and — on plans that allow it — subject, branding, intro/footer text, enabled sections, and custom CSS. Partial updates are supported; fields not allowed on the current plan are ignored. Read the current values with get-email-report-settings first.', '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' ); | |
| 36 | 33 | } |
| 37 | 34 | |
| 38 | 35 | /** |
| 39 | 36 | * {@inheritDoc} |
| @@ -42,9 +39,9 @@ | ||
| 42 | 39 | */ |
| 43 | 40 | public function get_annotations() { |
| 44 | 41 | return [ |
| 45 | 42 | 'readonly' => false, |
| 46 | - 'destructive' => true, | |
| 43 | + 'destructive' => false, | |
| 47 | 44 | 'idempotent' => true, |
| 48 | 45 | 'priority' => 2.0, |
| 49 | 46 | 'openWorldHint' => false, |
| 50 | 47 | ]; |
| @@ -59,35 +56,14 @@ | ||
| 59 | 56 | return [ |
| 60 | 57 | 'type' => 'object', |
| 61 | 58 | 'additionalProperties' => false, |
| 62 | 59 | 'properties' => [ |
| 63 | - 'settings' => [ | |
| 64 | - 'type' => 'object', | |
| 65 | - 'description' => __( 'Email Reporting settings to update (any subset of the fields below).', 'thinkrank' ), | |
| 66 | - 'additionalProperties' => false, | |
| 67 | - 'properties' => [ | |
| 68 | - 'enabled' => [ 'type' => 'boolean' ], | |
| 69 | - 'frequency_days' => [ | |
| 70 | - 'type' => 'integer', | |
| 71 | - 'description' => __( 'Days between reports. Free plans are clamped to 30.', 'thinkrank' ), | |
| 72 | - ], | |
| 73 | - 'recipients' => [ | |
| 74 | - 'type' => [ 'array', 'string', 'null' ], | |
| 75 | - 'description' => __( 'Recipient email(s), as an array or comma-separated string. Free plans are clamped to the site admin email.', 'thinkrank' ), | |
| 76 | - ], | |
| 77 | - 'subject_template' => [ 'type' => [ 'string', 'null' ] ], | |
| 78 | - 'logo_url' => [ 'type' => [ 'string', 'null' ] ], | |
| 79 | - 'logo_link' => [ 'type' => [ 'string', 'null' ] ], | |
| 80 | - 'header_background' => [ 'type' => [ 'string', 'null' ] ], | |
| 81 | - 'link_to_full_report' => [ 'type' => 'boolean' ], | |
| 82 | - 'intro_text' => [ 'type' => [ 'string', 'null' ] ], | |
| 83 | - 'sections_enabled' => [ 'type' => [ 'array', 'null' ] ], | |
| 84 | - 'footer_text' => [ 'type' => [ 'string', 'null' ] ], | |
| 85 | - 'additional_css' => [ 'type' => [ 'string', 'null' ] ], | |
| 86 | - ], | |
| 60 | + 'enabled' => [ | |
| 61 | + 'type' => 'boolean', | |
| 62 | + 'description' => __( 'Whether the scheduled report is sent.', 'thinkrank' ), | |
| 87 | 63 | ], |
| 88 | 64 | ], |
| 89 | - 'required' => [ 'settings' ], | |
| 65 | + 'required' => [ 'enabled' ], | |
| 90 | 66 | ]; |
| 91 | 67 | } |
| 92 | 68 | |
| 93 | 69 | /** |
| @@ -102,9 +78,9 @@ | ||
| 102 | 78 | 'success' => [ 'type' => 'boolean' ], |
| 103 | 79 | 'message' => [ 'type' => 'string' ], |
| 104 | 80 | 'config' => [ |
| 105 | 81 | 'type' => 'object', |
| 106 | - 'description' => __( 'The persisted config after sanitization and capability-clamping.', 'thinkrank' ), | |
| 82 | + 'description' => __( 'The resolved config after the write.', 'thinkrank' ), | |
| 107 | 83 | 'additionalProperties' => true, |
| 108 | 84 | ], |
| 109 | 85 | 'next_run' => [ 'type' => [ 'string', 'null' ] ], |
| 110 | 86 | ], |
| @@ -117,14 +93,12 @@ | ||
| 117 | 93 | * @param array<string, mixed> $input Ability input payload. |
| 118 | 94 | * @return array<string, mixed>|\WP_Error |
| 119 | 95 | */ |
| 120 | 96 | public function execute( $input ) { |
| 121 | - $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; | |
| 122 | - | |
| 123 | - if ( empty( $settings ) ) { | |
| 97 | + if ( ! is_array( $input ) || ! array_key_exists( 'enabled', $input ) ) { | |
| 124 | 98 | return new \WP_Error( |
| 125 | 99 | 'thinkrank_missing_email_report_settings_payload', |
| 126 | - __( 'An email report settings payload is required.', 'thinkrank' ), | |
| 100 | + __( 'The enabled flag is required.', 'thinkrank' ), | |
| 127 | 101 | [ 'status' => 400 ] |
| 128 | 102 | ); |
| 129 | 103 | } |
| 130 | 104 | |
| @@ -137,14 +111,15 @@ | ||
| 137 | 111 | [ 'status' => 500 ] |
| 138 | 112 | ); |
| 139 | 113 | } |
| 140 | 114 | |
| 141 | - // Email_Report_Config::save() sanitizes and capability-clamps every field. | |
| 142 | - $saved = $manager->config()->save( $settings ); | |
| 115 | + $saved = $manager->config()->save( [ 'enabled' => (bool) $input['enabled'] ] ); | |
| 143 | 116 | |
| 144 | 117 | return [ |
| 145 | 118 | 'success' => true, |
| 146 | - 'message' => __( 'Email report settings updated.', 'thinkrank' ), | |
| 119 | + 'message' => $saved['enabled'] | |
| 120 | + ? __( 'Email reporting is on.', 'thinkrank' ) | |
| 121 | + : __( 'Email reporting is off.', 'thinkrank' ), | |
| 147 | 122 | 'config' => $saved, |
| 148 | 123 | 'next_run' => $manager->scheduler()->next_run_iso(), |
| 149 | 124 | ]; |
| 150 | 125 | } |