| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Display Player element. |
| 7 |
* |
| 8 |
* @package Beyondwords\Wordpress |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Beyondwords\Wordpress\Component\Post\DisplayPlayer; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 16 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* PostMetabox setup |
| 20 |
* |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
class DisplayPlayer |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Constructor |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
add_action('wp_loaded', function () { |
| 31 |
$postTypes = SettingsUtils::getSupportedPostTypes(); |
| 32 |
|
| 33 |
if (is_array($postTypes)) { |
| 34 |
foreach ($postTypes as $postType) { |
| 35 |
add_action("save_post_{$postType}", array($this, 'save'), 20); |
| 36 |
} |
| 37 |
} |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Save the meta when the post is saved. |
| 43 |
* |
| 44 |
* @param int $postId The ID of the post being saved. |
| 45 |
*/ |
| 46 |
public function save($postId) |
| 47 |
{ |
| 48 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 49 |
return $postId; |
| 50 |
} |
| 51 |
|
| 52 |
// "save_post" can be triggered at other times, so verify this request came from the our component |
| 53 |
if ( |
| 54 |
! isset($_POST['beyondwords_display_player_nonce']) || |
| 55 |
! wp_verify_nonce( |
| 56 |
sanitize_text_field($_POST['beyondwords_display_player_nonce']), |
| 57 |
'beyondwords_display_player' |
| 58 |
) |
| 59 |
) { |
| 60 |
return $postId; |
| 61 |
} |
| 62 |
|
| 63 |
if (isset($_POST['beyondwords_display_player'])) { |
| 64 |
update_post_meta($postId, 'beyondwords_disabled', ''); |
| 65 |
} else { |
| 66 |
update_post_meta($postId, 'beyondwords_disabled', '1'); |
| 67 |
} |
| 68 |
|
| 69 |
return $postId; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Render the element. |
| 74 |
* |
| 75 |
* @param WP_Post $post The post object. |
| 76 |
*/ |
| 77 |
public function element($post) |
| 78 |
{ |
| 79 |
if (!($post instanceof \WP_Post)) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
wp_nonce_field('beyondwords_display_player', 'beyondwords_display_player_nonce'); |
| 84 |
|
| 85 |
$displayPlayer = PostMetaUtils::getDisabled($post->ID) !== '1'; |
| 86 |
?> |
| 87 |
<!-- checkbox --> |
| 88 |
<p id="beyondwords-metabox-display-player"> |
| 89 |
<input |
| 90 |
type="checkbox" |
| 91 |
id="beyondwords_display_player" |
| 92 |
name="beyondwords_display_player" |
| 93 |
value="1" |
| 94 |
<?php checked($displayPlayer, true); ?> |
| 95 |
/> |
| 96 |
<?php _e('Display player', 'speechkit'); ?> |
| 97 |
</p> |
| 98 |
<?php |
| 99 |
} |
| 100 |
} |
| 101 |
|