| 1 |
<?php |
| 2 |
/** |
| 3 |
* Delete an FAQ ability. |
| 4 |
* |
| 5 |
* @package BetterDocs |
| 6 |
* @since 4.9.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDeveloper\BetterDocs\Abilities\Faq; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
use WPDeveloper\BetterDocs\Abilities\AbilityBase; |
| 16 |
use WPDeveloper\BetterDocs\Abilities\AbilityError; |
| 17 |
use WPDeveloper\BetterDocs\Abilities\Traits\ShapesFAQs; |
| 18 |
|
| 19 |
/** |
| 20 |
* Remove one FAQ. **Permanently.** |
| 21 |
* |
| 22 |
* `FAQBuilder::delete_betterdocs_faq()` calls `wp_delete_post()` without |
| 23 |
* forcing, which for `post` and `page` would mean the trash — but core routes |
| 24 |
* only those two post types there, so a custom post type like |
| 25 |
* `betterdocs_faq` is deleted outright. Measured on WordPress 7.1: the FAQ is |
| 26 |
* gone from the database and a second call answers `not_found`. |
| 27 |
* |
| 28 |
* The answer still reports `trashed`, from the post's state afterwards, so a |
| 29 |
* site whose plugins do intervene is described accurately rather than assumed. |
| 30 |
* |
| 31 |
* @since 4.9.0 |
| 32 |
*/ |
| 33 |
class DeleteFAQ extends AbilityBase { |
| 34 |
|
| 35 |
use ShapesFAQs; |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 4.9.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
$this->id = 'betterdocs/delete-faq'; |
| 42 |
$this->label = __( 'Delete FAQ', 'betterdocs' ); |
| 43 |
$this->description = __( 'Delete a BetterDocs FAQ. This is permanent on a standard site: WordPress sends only posts and pages to the trash, so a custom post type like an FAQ is removed outright. The answer reports whether it was trashed or deleted.', 'betterdocs' ); |
| 44 |
$this->capability = 'edit_others_docs'; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @since 4.9.0 |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function get_annotations() { |
| 53 |
return [ |
| 54 |
'readonly' => false, |
| 55 |
'destructive' => true, |
| 56 |
'idempotent' => false, |
| 57 |
'priority' => 2.5, |
| 58 |
'openWorldHint' => false |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @since 4.9.0 |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function get_input_schema() { |
| 68 |
return [ |
| 69 |
'type' => 'object', |
| 70 |
'additionalProperties' => false, |
| 71 |
'required' => [ 'id' ], |
| 72 |
'properties' => [ |
| 73 |
'id' => [ |
| 74 |
'type' => 'integer', |
| 75 |
'description' => __( 'The FAQ id. Required.', 'betterdocs' ) |
| 76 |
] |
| 77 |
], |
| 78 |
'default' => [] |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @since 4.9.0 |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_output_schema() { |
| 88 |
return [ |
| 89 |
'type' => 'object', |
| 90 |
'properties' => [ |
| 91 |
'id' => [ 'type' => 'integer' ], |
| 92 |
'deleted' => [ 'type' => 'boolean' ], |
| 93 |
'trashed' => [ 'type' => 'boolean' ], |
| 94 |
'question' => [ 'type' => 'string' ] |
| 95 |
] |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @since 4.9.0 |
| 101 |
* |
| 102 |
* @param array $input Validated input. |
| 103 |
* @return array|\WP_Error |
| 104 |
*/ |
| 105 |
public function execute( $input ) { |
| 106 |
$id = isset( $input['id'] ) ? (int) $input['id'] : 0; |
| 107 |
$post = $id > 0 ? get_post( $id ) : null; |
| 108 |
|
| 109 |
if ( ! $post || self::FAQ_POST_TYPE !== $post->post_type ) { |
| 110 |
return AbilityError::not_found( 'FAQ', $id ); |
| 111 |
} |
| 112 |
|
| 113 |
$question = (string) $post->post_title; |
| 114 |
|
| 115 |
$result = $this->dispatch( 'POST', '/faq/delete_post', [ 'post_id' => $id ], self::FAQ_NS ); |
| 116 |
|
| 117 |
if ( is_wp_error( $result ) ) { |
| 118 |
return $this->map_faq_error( $result, __( 'delete an FAQ', 'betterdocs' ) ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( empty( $result ) ) { |
| 122 |
return AbilityError::upstream( __( 'BetterDocs did not confirm the FAQ deletion.', 'betterdocs' ) ); |
| 123 |
} |
| 124 |
|
| 125 |
$after = get_post( $id ); |
| 126 |
|
| 127 |
return [ |
| 128 |
'id' => $id, |
| 129 |
'deleted' => true, |
| 130 |
'trashed' => (bool) ( $after && 'trash' === $after->post_status ), |
| 131 |
'question' => $question |
| 132 |
]; |
| 133 |
} |
| 134 |
} |
| 135 |
|