PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.10.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.10.0
2.10.0 2.9.0 2.8.0 2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 All 51 releases
thinkrank / includes / abilities / settings / class-send-email-report-test.php

class-send-email-report-test.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.10.0, at includes/abilities/settings/class-send-email-report-test.php

145 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Send test email report 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 * Sends an immediate one-off test of the ThinkRank Email Report.
21 *
22 * Mirrors `POST /thinkrank/v1/email-report/test-send`: generates the report
23 * from the current saved config and emails it right now to the configured
24 * recipients, without touching the schedule. Fails when no recipients are
25 * configured. This actually delivers an email, so it is a non-idempotent,
26 * side-effecting action.
27 */
28 class Send_Email_Report_Test extends Ability_Base {
29 /**
30 * Constructor.
31 */
32 public function __construct() {
33 $this->id = 'thinkrank/send-email-report-test';
34 $this->label = __( 'Send ThinkRank Test Email Report', 'thinkrank' );
35 $this->description = __( 'Send an immediate one-off test of the ThinkRank Email Report to the configured recipients using the current saved settings. Does not change the schedule. Fails if no recipients are configured. update-email-report-settings changes what it sends.', 'thinkrank' );
36 }
37
38 /**
39 * {@inheritDoc}
40 *
41 * @return array<string, bool|float|string>
42 */
43 public function get_annotations() {
44 return [
45 'readonly' => false,
46 'destructive' => false,
47 'idempotent' => false,
48 'priority' => 2.0,
49 'openWorldHint' => false,
50 ];
51 }
52
53 /**
54 * {@inheritDoc}
55 *
56 * @return array<string, mixed>
57 */
58 public function get_input_schema() {
59 return [
60 'type' => 'object',
61 'additionalProperties' => false,
62 'properties' => self::empty_properties(),
63 ];
64 }
65
66 /**
67 * {@inheritDoc}
68 *
69 * @return array<string, mixed>
70 */
71 public function get_output_schema() {
72 return [
73 'type' => 'object',
74 'properties' => [
75 'success' => [ 'type' => 'boolean' ],
76 'message' => [ 'type' => 'string' ],
77 'result' => [
78 'type' => 'object',
79 'description' => __( 'Raw generator result: send status, error or skip reason, and not_connected (true when the test went out as the "connect Search Console" email because Google Search Console is not connected).', 'thinkrank' ),
80 'additionalProperties' => true,
81 ],
82 ],
83 ];
84 }
85
86 /**
87 * Execute ability.
88 *
89 * @param array<string, mixed> $input Ability input payload.
90 * @return array<string, mixed>|\WP_Error
91 */
92 public function execute( $input ) {
93 $manager = $this->resolve_manager();
94
95 if ( ! $manager instanceof Email_Report_Manager ) {
96 return new \WP_Error(
97 'thinkrank_email_report_unavailable',
98 __( 'Email Report Manager is unavailable.', 'thinkrank' ),
99 [ 'status' => 500 ]
100 );
101 }
102
103 $result = $manager->generator()->generate_test();
104 $success = ! empty( $result['success'] );
105
106 if ( ! $success ) {
107 $message = isset( $result['error'] ) && is_string( $result['error'] )
108 ? (string) $result['error']
109 : __( 'The test report could not be sent.', 'thinkrank' );
110
111 return new \WP_Error(
112 'thinkrank_email_report_test_send_failed',
113 $message,
114 [
115 'status' => 400,
116 'result' => $result,
117 ]
118 );
119 }
120
121 return [
122 'success' => true,
123 'message' => ! empty( $result['not_connected'] )
124 ? __( 'Test email sent. Google Search Console is not connected, so it explains how to connect rather than showing a report; scheduled reports are paused until then.', 'thinkrank' )
125 : __( 'Test email report sent.', 'thinkrank' ),
126 'result' => $result,
127 ];
128 }
129
130 /**
131 * Resolve the shared Email_Report_Manager from the plugin container.
132 *
133 * @return Email_Report_Manager|null
134 */
135 private function resolve_manager() {
136 if ( ! function_exists( 'thinkrank' ) ) {
137 return null;
138 }
139
140 $component = thinkrank()->get_component( 'email_report' );
141
142 return $component instanceof Email_Report_Manager ? $component : null;
143 }
144 }
145