| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Component: Content ID |
| 7 |
* |
| 8 |
* @package BeyondWords\Editor\Components |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 6.3.0 |
| 11 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace BeyondWords\Editor\Components; |
| 15 |
|
| 16 |
/** |
| 17 |
* ContentId |
| 18 |
* |
| 19 |
* @since 6.3.0 |
| 20 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 21 |
*/ |
| 22 |
defined( 'ABSPATH' ) || exit; |
| 23 |
|
| 24 |
class ContentId { |
| 25 |
|
| 26 |
/** |
| 27 |
* Init. |
| 28 |
* |
| 29 |
* @since 6.3.0 |
| 30 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 31 |
*/ |
| 32 |
public static function init() { |
| 33 |
add_action( |
| 34 |
'wp_loaded', |
| 35 |
function (): void { |
| 36 |
$post_types = \BeyondWords\Settings\Utils::get_compatible_post_types(); |
| 37 |
|
| 38 |
if ( is_array( $post_types ) ) { |
| 39 |
foreach ( $post_types as $post_type ) { |
| 40 |
add_action( "save_post_{$post_type}", [ self::class, 'save'], 10 ); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* HTML output for this component. |
| 49 |
* |
| 50 |
* @since 6.3.0 |
| 51 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 52 |
* |
| 53 |
* @param \WP_Post $post The post object. |
| 54 |
*/ |
| 55 |
public static function element( $post ) { |
| 56 |
$content_id = \BeyondWords\Post\Meta::get_content_id( $post->ID ) ?: ''; |
| 57 |
$project_id = \BeyondWords\Post\Meta::get_project_id( $post->ID ) ?: get_option( 'beyondwords_project_id', '' ); |
| 58 |
$post_type = get_post_type( $post ); |
| 59 |
$post_type_object = $post_type ? get_post_type_object( $post_type ) : null; |
| 60 |
$rest_base = ( $post_type_object && ! empty( $post_type_object->rest_base ) ) ? $post_type_object->rest_base : $post_type; |
| 61 |
|
| 62 |
wp_nonce_field( 'beyondwords_content_id', 'beyondwords_content_id_nonce' ); |
| 63 |
?> |
| 64 |
<div id="beyondwords-metabox-content-id"> |
| 65 |
<p class="post-attributes-label-wrapper"> |
| 66 |
<label for="beyondwords_content_id" class="post-attributes-label"> |
| 67 |
<?php esc_html_e( 'Content ID', 'speechkit' ); ?> |
| 68 |
</label> |
| 69 |
</p> |
| 70 |
<input |
| 71 |
type="text" |
| 72 |
id="beyondwords_content_id" |
| 73 |
name="beyondwords_content_id" |
| 74 |
class="beyondwords-metabox-content-id__input" |
| 75 |
value="<?php echo esc_attr( $content_id ); ?>" |
| 76 |
/> |
| 77 |
<div class="beyondwords-metabox-content-id__actions"> |
| 78 |
<button |
| 79 |
type="button" |
| 80 |
id="beyondwords__content-id--fetch" |
| 81 |
class="button" |
| 82 |
data-project-id="<?php echo esc_attr( $project_id ); ?>" |
| 83 |
data-rest-base="<?php echo esc_attr( $rest_base ); ?>" |
| 84 |
> |
| 85 |
<?php esc_html_e( 'Fetch', 'speechkit' ); ?> |
| 86 |
</button> |
| 87 |
</div> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Save the meta when the post is saved. |
| 94 |
* |
| 95 |
* @since 6.3.0 |
| 96 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 97 |
* |
| 98 |
* @param int $post_id The ID of the post being saved. |
| 99 |
*/ |
| 100 |
public static function save( $post_id ) { |
| 101 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 102 |
return $post_id; |
| 103 |
} |
| 104 |
|
| 105 |
if ( |
| 106 |
! isset( $_POST['beyondwords_content_id_nonce'] ) || |
| 107 |
! wp_verify_nonce( |
| 108 |
sanitize_key( $_POST['beyondwords_content_id_nonce'] ), |
| 109 |
'beyondwords_content_id' |
| 110 |
) |
| 111 |
) { |
| 112 |
return $post_id; |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 116 |
return $post_id; |
| 117 |
} |
| 118 |
|
| 119 |
if ( isset( $_POST['beyondwords_content_id'] ) ) { |
| 120 |
// Content IDs are interpolated into API URL paths, so they need a stricter |
| 121 |
// charset than sanitize_text_field() — see Meta::sanitize_content_id(). |
| 122 |
update_post_meta( |
| 123 |
$post_id, |
| 124 |
'beyondwords_content_id', |
| 125 |
\BeyondWords\Post\Meta::sanitize_content_id( |
| 126 |
sanitize_text_field( wp_unslash( $_POST['beyondwords_content_id'] ) ) |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
return $post_id; |
| 132 |
} |
| 133 |
} |
| 134 |
|