| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Component: Player content |
| 7 |
* |
| 8 |
* @package Beyondwords\Wordpress |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 5.3.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Beyondwords\Wordpress\Component\Post\PlayerContent; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 16 |
|
| 17 |
/** |
| 18 |
* PlayerContent |
| 19 |
* |
| 20 |
* @since 5.3.0 |
| 21 |
*/ |
| 22 |
class PlayerContent |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Options. |
| 26 |
* |
| 27 |
* @since 5.3.0 Introduced. |
| 28 |
* |
| 29 |
* @var array Associative array of player content values and labels. |
| 30 |
*/ |
| 31 |
public const OPTIONS = [ |
| 32 |
[ |
| 33 |
'value' => '', |
| 34 |
'label' => 'Article' |
| 35 |
], |
| 36 |
[ |
| 37 |
'value' => 'summary', |
| 38 |
'label' => 'Summary' |
| 39 |
], |
| 40 |
]; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
* |
| 45 |
* @since 5.3.0 Introduced. |
| 46 |
* @since 6.0.0 Make static. |
| 47 |
*/ |
| 48 |
public static function init() |
| 49 |
{ |
| 50 |
add_action('wp_loaded', function (): void { |
| 51 |
$postTypes = SettingsUtils::getCompatiblePostTypes(); |
| 52 |
|
| 53 |
if (is_array($postTypes)) { |
| 54 |
foreach ($postTypes as $postType) { |
| 55 |
add_action("save_post_{$postType}", [self::class, 'save'], 10); |
| 56 |
} |
| 57 |
} |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* HTML output for this component. |
| 63 |
* |
| 64 |
* @since 5.3.0 Introduced. |
| 65 |
* @since 6.0.0 Make static. |
| 66 |
* |
| 67 |
* @param \WP_Post $post The post object. |
| 68 |
* |
| 69 |
* @return string|null |
| 70 |
*/ |
| 71 |
public static function element($post) |
| 72 |
{ |
| 73 |
$playerContent = get_post_meta($post->ID, 'beyondwords_player_content', true); |
| 74 |
|
| 75 |
wp_nonce_field('beyondwords_player_content', 'beyondwords_player_content_nonce'); |
| 76 |
?> |
| 77 |
<p |
| 78 |
id="beyondwords-metabox-player-content" |
| 79 |
class="post-attributes-label-wrapper page-template-label-wrapper" |
| 80 |
> |
| 81 |
<label class="post-attributes-label" for="beyondwords_player_content"> |
| 82 |
<?php esc_html_e('Player content', 'speechkit'); ?> |
| 83 |
</label> |
| 84 |
</p> |
| 85 |
<select id="beyondwords_player_content" name="beyondwords_player_content" style="width: 100%;"> |
| 86 |
<?php |
| 87 |
foreach (self::OPTIONS as $option) { |
| 88 |
printf( |
| 89 |
'<option value="%s" %s %s>%s</option>', |
| 90 |
esc_attr($option['value']), |
| 91 |
selected(strval($option['value']), $playerContent), |
| 92 |
disabled($option['disabled'] ?? false, true), |
| 93 |
esc_html($option['label']) |
| 94 |
); |
| 95 |
} |
| 96 |
?> |
| 97 |
</select> |
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Save the meta when the post is saved. |
| 103 |
* |
| 104 |
* @since 5.3.0 Introduced. |
| 105 |
* @since 6.0.0 Make static. |
| 106 |
* |
| 107 |
* @param int $postId The ID of the post being saved. |
| 108 |
*/ |
| 109 |
public static function save($postId) |
| 110 |
{ |
| 111 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 112 |
return $postId; |
| 113 |
} |
| 114 |
|
| 115 |
// "save_post" can be triggered at other times, so verify this request came from the our component |
| 116 |
if (! isset($_POST['beyondwords_player_content']) || ! isset($_POST['beyondwords_player_content_nonce'])) { |
| 117 |
return $postId; |
| 118 |
} |
| 119 |
|
| 120 |
// "save_post" can be triggered at other times, so verify this request came from the our component |
| 121 |
if ( |
| 122 |
! wp_verify_nonce( |
| 123 |
sanitize_key($_POST['beyondwords_player_content_nonce']), |
| 124 |
'beyondwords_player_content' |
| 125 |
) |
| 126 |
) { |
| 127 |
return $postId; |
| 128 |
} |
| 129 |
|
| 130 |
$playerContent = sanitize_text_field(wp_unslash($_POST['beyondwords_player_content'])); |
| 131 |
|
| 132 |
if (! empty($playerContent)) { |
| 133 |
update_post_meta($postId, 'beyondwords_player_content', $playerContent); |
| 134 |
} else { |
| 135 |
delete_post_meta($postId, 'beyondwords_player_content'); |
| 136 |
} |
| 137 |
|
| 138 |
return $postId; |
| 139 |
} |
| 140 |
} |
| 141 |
|