| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update thin content settings ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Content |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Content; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Thin_Content; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Sets the length below which a page counts as thin, in general and per post |
| 21 |
* type (#565). |
| 22 |
*/ |
| 23 |
class Update_Thin_Content_Settings extends Ability_Base { |
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->id = 'thinkrank/update-thin-content-settings'; |
| 29 |
$this->label = __( 'Update ThinkRank Thin Content Settings', 'thinkrank' ); |
| 30 |
$this->description = __( 'Set the length below which a published page counts as thin: a default for the site, and an override per post type for content that is short by nature, such as products or staff profiles. Send null for a post type to clear its override and return it to the default. Lengths are in words, or in characters for locales that count characters. Changing a threshold re-reads the existing counts and never recounts the site, so it takes effect immediately; call get-thin-content afterwards to see the report under the new numbers.', 'thinkrank' ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* {@inheritDoc} |
| 35 |
* |
| 36 |
* @return array<string, bool|float|string> |
| 37 |
*/ |
| 38 |
public function get_annotations() { |
| 39 |
return [ |
| 40 |
'readonly' => false, |
| 41 |
// Changes what the report calls thin. It loses no content and no |
| 42 |
// stored count, and it is reversible by sending the old number |
| 43 |
// back, so it is not destructive: marking routine settings so |
| 44 |
// makes an MCP client demand approval for every call (#675). |
| 45 |
'destructive' => false, |
| 46 |
'idempotent' => true, |
| 47 |
'priority' => 0.5, |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
* |
| 54 |
* @return array<string, mixed> |
| 55 |
*/ |
| 56 |
public function get_input_schema() { |
| 57 |
$properties = []; |
| 58 |
foreach ( Thin_Content::post_types() as $post_type ) { |
| 59 |
$properties[ $post_type ] = [ |
| 60 |
'type' => [ 'integer', 'null' ], |
| 61 |
'minimum' => Thin_Content::MIN_THRESHOLD, |
| 62 |
'maximum' => Thin_Content::MAX_THRESHOLD, |
| 63 |
/* translators: %s: post type slug. */ |
| 64 |
'description' => sprintf( __( 'Threshold for %s, or null to use the default.', 'thinkrank' ), $post_type ), |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
return [ |
| 69 |
'type' => 'object', |
| 70 |
'additionalProperties' => false, |
| 71 |
'properties' => [ |
| 72 |
'default' => [ |
| 73 |
'type' => 'integer', |
| 74 |
'minimum' => Thin_Content::MIN_THRESHOLD, |
| 75 |
'maximum' => Thin_Content::MAX_THRESHOLD, |
| 76 |
'description' => __( 'Threshold for every post type without an override. 300 by default, the same number the Site SEO Analyzer depth check uses.', 'thinkrank' ), |
| 77 |
], |
| 78 |
'overrides' => [ |
| 79 |
'type' => 'object', |
| 80 |
'additionalProperties' => false, |
| 81 |
'properties' => $properties, |
| 82 |
'description' => __( 'Per-post-type thresholds. Only the post types sent are changed; the rest keep what they have.', 'thinkrank' ), |
| 83 |
], |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* {@inheritDoc} |
| 90 |
* |
| 91 |
* @return array<string, mixed> |
| 92 |
*/ |
| 93 |
public function get_output_schema() { |
| 94 |
return [ |
| 95 |
'type' => 'object', |
| 96 |
'properties' => [ |
| 97 |
'default' => [ 'type' => 'integer' ], |
| 98 |
'overrides' => [ |
| 99 |
'type' => 'object', |
| 100 |
'description' => __( 'Post types with a threshold of their own.', 'thinkrank' ), |
| 101 |
], |
| 102 |
'effective' => [ |
| 103 |
'type' => 'object', |
| 104 |
'description' => __( 'The threshold now in force for every post type in scope, override or default.', 'thinkrank' ), |
| 105 |
], |
| 106 |
'unit' => [ 'type' => 'string' ], |
| 107 |
], |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Execute ability. |
| 113 |
* |
| 114 |
* @param array<string, mixed> $input Ability input payload. |
| 115 |
* @return array<string, mixed>|\WP_Error |
| 116 |
*/ |
| 117 |
public function execute( $input ) { |
| 118 |
$payload = []; |
| 119 |
|
| 120 |
if ( isset( $input['default'] ) ) { |
| 121 |
$payload['default'] = (int) $input['default']; |
| 122 |
} |
| 123 |
|
| 124 |
if ( isset( $input['overrides'] ) && is_array( $input['overrides'] ) ) { |
| 125 |
$payload['overrides'] = $input['overrides']; |
| 126 |
} |
| 127 |
|
| 128 |
if ( empty( $payload ) ) { |
| 129 |
return new \WP_Error( |
| 130 |
'thinkrank_nothing_to_update', |
| 131 |
__( 'Send a default, an override, or both.', 'thinkrank' ), |
| 132 |
[ 'status' => 400 ] |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
$stored = Thin_Content::save_settings( $payload ); |
| 137 |
|
| 138 |
// What is stored, not what was sent: a value the clamp reduced is |
| 139 |
// reported as the value it became, never as a successful save of |
| 140 |
// something that was not kept. |
| 141 |
return [ |
| 142 |
'default' => $stored['default'], |
| 143 |
'overrides' => (object) $stored['overrides'], |
| 144 |
'effective' => (object) Thin_Content::thresholds(), |
| 145 |
'unit' => \ThinkRank\SEO\Word_Count_Index::unit(), |
| 146 |
]; |
| 147 |
} |
| 148 |
} |
| 149 |
|