| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update external link settings ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Links |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Links; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Writes the site-wide outbound-link handling settings. |
| 18 |
* |
| 19 |
* Mirrors `POST /thinkrank/v1/external-links/settings`, including its |
| 20 |
* normalisation: an exception typed as a full URL, or with a "www." prefix, is |
| 21 |
* reduced to the host the matcher compares against. The stored values are |
| 22 |
* returned rather than the submitted ones, so a caller can see what it actually |
| 23 |
* saved instead of assuming its input survived intact. |
| 24 |
* |
| 25 |
* Only the fields sent are changed; omitted fields keep their stored value. |
| 26 |
*/ |
| 27 |
class Update_External_Links_Settings extends External_Links_Ability_Base { |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->id = 'thinkrank/update-external-links-settings'; |
| 34 |
$this->label = __( 'Update ThinkRank External Link Settings', 'thinkrank' ); |
| 35 |
$this->description = __( 'Set the site-wide outbound-link settings: rel="nofollow" on external links, opening them in a new tab, and the list of excluded hosts. Only the fields you send are changed. Exceptions are normalised to bare hosts, and the stored values are returned so you can see what was saved. Call get-external-links-settings to read the current values first.', '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' => true, |
| 48 |
'priority' => 0.8, |
| 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' => $this->settings_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' => array_merge( |
| 75 |
[ 'success' => [ 'type' => 'boolean' ] ], |
| 76 |
$this->settings_properties() |
| 77 |
), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Execute ability. |
| 83 |
* |
| 84 |
* @param array<string, mixed> $input Ability input payload. |
| 85 |
* @return array<string, mixed>|\WP_Error |
| 86 |
*/ |
| 87 |
public function execute( $input ) { |
| 88 |
$input = (array) $input; |
| 89 |
$payload = []; |
| 90 |
|
| 91 |
foreach ( [ 'nofollow_external', 'open_external_in_new_tab' ] as $key ) { |
| 92 |
if ( array_key_exists( $key, $input ) ) { |
| 93 |
$payload[ $key ] = (bool) $input[ $key ]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( array_key_exists( 'external_link_exceptions', $input ) ) { |
| 98 |
$payload['external_link_exceptions'] = $this->manager()->normalize_exceptions( |
| 99 |
(array) $input['external_link_exceptions'] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! $payload ) { |
| 104 |
return new \WP_Error( |
| 105 |
'thinkrank_no_settings_provided', |
| 106 |
__( 'Provide at least one setting to change.', 'thinkrank' ), |
| 107 |
[ 'status' => 400 ] |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! $this->manager()->save_settings( 'site', 0, $payload ) ) { |
| 112 |
$error = $this->manager()->get_last_save_error(); |
| 113 |
|
| 114 |
return new \WP_Error( |
| 115 |
'thinkrank_external_links_save_failed', |
| 116 |
'' !== $error ? $error : __( 'Failed to update the external link settings.', 'thinkrank' ), |
| 117 |
[ 'status' => 500 ] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
$settings = $this->stored(); |
| 122 |
|
| 123 |
return [ |
| 124 |
'success' => true, |
| 125 |
'nofollow_external' => (bool) ( $settings['nofollow_external'] ?? false ), |
| 126 |
'open_external_in_new_tab' => (bool) ( $settings['open_external_in_new_tab'] ?? false ), |
| 127 |
'external_link_exceptions' => $settings['external_link_exceptions'], |
| 128 |
]; |
| 129 |
} |
| 130 |
} |
| 131 |
|