| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Component: Generate audio |
| 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\GenerateAudio; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 16 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* GenerateAudio |
| 20 |
* |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
defined('ABSPATH') || exit; |
| 24 |
|
| 25 |
class GenerateAudio |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Init. |
| 29 |
* |
| 30 |
* @since 4.0.0 |
| 31 |
* @since 6.0.0 Make static. |
| 32 |
*/ |
| 33 |
public static function init() |
| 34 |
{ |
| 35 |
add_action('wp_loaded', function (): void { |
| 36 |
$postTypes = SettingsUtils::getCompatiblePostTypes(); |
| 37 |
|
| 38 |
if (is_array($postTypes)) { |
| 39 |
foreach ($postTypes as $postType) { |
| 40 |
add_action("save_post_{$postType}", [self::class, 'save'], 10); |
| 41 |
} |
| 42 |
} |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check whether the post type should preselect the "Generate audio" checkbox. |
| 48 |
* |
| 49 |
* @param \WP_Post|int $post The post object or ID. |
| 50 |
* |
| 51 |
* @todo move this function to somewhere reusable for the Block editor. |
| 52 |
* |
| 53 |
* @since 6.0.0 Make static. |
| 54 |
*/ |
| 55 |
public static function shouldPreselectGenerateAudio($post) |
| 56 |
{ |
| 57 |
$postType = get_post_type($post); |
| 58 |
|
| 59 |
if (! $postType) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$preselect = get_option('beyondwords_preselect'); |
| 64 |
|
| 65 |
if (! is_array($preselect)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
// Preselect if the post type in the settings has been checked (not the taxonomies) |
| 70 |
if (array_key_exists($postType, $preselect) && $preselect[$postType] === '1') { |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Render the element. |
| 79 |
* |
| 80 |
* @since 6.0.0 Make static and refactor generate audio check. |
| 81 |
*/ |
| 82 |
public static function element($post) |
| 83 |
{ |
| 84 |
wp_nonce_field('beyondwords_generate_audio', 'beyondwords_generate_audio_nonce'); |
| 85 |
|
| 86 |
$generateAudio = PostMetaUtils::hasGenerateAudio($post->ID); |
| 87 |
?> |
| 88 |
<!-- checkbox --> |
| 89 |
<p id="beyondwords-metabox-generate-audio"> |
| 90 |
<input |
| 91 |
type="checkbox" |
| 92 |
id="beyondwords_generate_audio" |
| 93 |
name="beyondwords_generate_audio" |
| 94 |
value="1" |
| 95 |
<?php checked($generateAudio); ?> |
| 96 |
/> |
| 97 |
<?php esc_html_e('Generate audio', 'speechkit'); ?> |
| 98 |
</p> |
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Save the meta when the post is saved. |
| 104 |
* |
| 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 ( |
| 117 |
! isset($_POST['beyondwords_generate_audio_nonce']) || |
| 118 |
! wp_verify_nonce( |
| 119 |
sanitize_key($_POST['beyondwords_generate_audio_nonce']), |
| 120 |
'beyondwords_generate_audio' |
| 121 |
) |
| 122 |
) { |
| 123 |
return $postId; |
| 124 |
} |
| 125 |
|
| 126 |
if (isset($_POST['beyondwords_generate_audio'])) { |
| 127 |
update_post_meta($postId, 'beyondwords_generate_audio', '1'); |
| 128 |
} else { |
| 129 |
delete_post_meta($postId, 'speechkit_error_message'); |
| 130 |
delete_post_meta($postId, 'beyondwords_error_message'); |
| 131 |
update_post_meta($postId, 'beyondwords_generate_audio', '0'); |
| 132 |
} |
| 133 |
|
| 134 |
return $postId; |
| 135 |
} |
| 136 |
} |
| 137 |
|