| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Post Metabox. |
| 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\Metabox; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\GenerateAudio\GenerateAudio; |
| 16 |
use Beyondwords\Wordpress\Component\DisplayPlayer\DisplayPlayer; |
| 17 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 18 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 19 |
use Beyondwords\Wordpress\Core\Environment; |
| 20 |
|
| 21 |
/** |
| 22 |
* PostMetabox setup |
| 23 |
* |
| 24 |
* @since 3.0.0 |
| 25 |
*/ |
| 26 |
class Metabox |
| 27 |
{ |
| 28 |
public $generateAudio; |
| 29 |
|
| 30 |
public $displayPlayer; |
| 31 |
|
| 32 |
public $selectVoice; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor |
| 36 |
*/ |
| 37 |
public function __construct($generateAudio, $displayPlayer, $selectVoice) |
| 38 |
{ |
| 39 |
$this->generateAudio = $generateAudio; |
| 40 |
$this->displayPlayer = $displayPlayer; |
| 41 |
$this->selectVoice = $selectVoice; |
| 42 |
|
| 43 |
add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts')); |
| 44 |
add_action("add_meta_boxes", array($this, 'addMetaBox')); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Enque JS for Bulk Edit feature. |
| 49 |
*/ |
| 50 |
public function adminEnqueueScripts($hook) |
| 51 |
{ |
| 52 |
// Only enqueue for Post screens |
| 53 |
if ($hook === 'post.php' || $hook === 'post-new.php') { |
| 54 |
// Register the Classic Editor "Metabox" CSS |
| 55 |
wp_enqueue_style( |
| 56 |
'beyondwords-Metabox', |
| 57 |
BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/Metabox/Metabox.css', |
| 58 |
false, |
| 59 |
BEYONDWORDS__PLUGIN_VERSION |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Adds the meta box container. |
| 66 |
* |
| 67 |
* @param $postType |
| 68 |
*/ |
| 69 |
public function addMetaBox($postType) |
| 70 |
{ |
| 71 |
$postTypes = SettingsUtils::getSupportedPostTypes(); |
| 72 |
|
| 73 |
if (is_array($postTypes) && ! in_array($postType, $postTypes)) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
add_meta_box( |
| 78 |
'beyondwords', |
| 79 |
__('BeyondWords', 'speechkit'), |
| 80 |
array($this, 'renderMetaBoxContent'), |
| 81 |
$postType, |
| 82 |
'side', |
| 83 |
'default', |
| 84 |
[ |
| 85 |
'__back_compat_meta_box' => true, |
| 86 |
] |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Render Meta Box content. |
| 92 |
* |
| 93 |
* @param WP_Post $post The post object. |
| 94 |
* |
| 95 |
* @since 3.0.0 |
| 96 |
* @since 3.7.0 Show "Pending review" notice for posts with status of "pending". |
| 97 |
* @since 4.0.0 Content ID is no longer an int |
| 98 |
*/ |
| 99 |
public function renderMetaBoxContent($post) |
| 100 |
{ |
| 101 |
if (!($post instanceof \WP_Post)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$contentId = PostMetaUtils::getContentId($post->ID); |
| 106 |
|
| 107 |
if ($contentId) { |
| 108 |
if (get_post_status($post) === 'pending') { |
| 109 |
$this->pendingReviewNotice($post); |
| 110 |
} else { |
| 111 |
$this->playerEmbed($post); |
| 112 |
} |
| 113 |
echo '<hr />'; |
| 114 |
$this->displayPlayer->element($post); |
| 115 |
} else { |
| 116 |
$this->errors($post); |
| 117 |
$this->generateAudio->element($post); |
| 118 |
$this->selectVoice->element($post); |
| 119 |
} |
| 120 |
|
| 121 |
echo '<hr />'; |
| 122 |
$this->help(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* The "Pending review" message, shown instead of the audio player |
| 128 |
* if the post status in WordPress is "pending". |
| 129 |
* |
| 130 |
* This message is displayed instead of the player because the player |
| 131 |
* cannot be rendered for audio which has been created |
| 132 |
* with { published: false }. |
| 133 |
* |
| 134 |
* @since 3.7.0 |
| 135 |
* |
| 136 |
* @var \WP_Post $post Post. |
| 137 |
*/ |
| 138 |
public function pendingReviewNotice($post) |
| 139 |
{ |
| 140 |
$projectUrl = sprintf( |
| 141 |
'%s/dashboard/project/%d/content', |
| 142 |
Environment::getDashboardUrl(), |
| 143 |
PostMetaUtils::getProjectId($post) |
| 144 |
); |
| 145 |
|
| 146 |
?> |
| 147 |
<div id="beyondwords-pending-review-message"> |
| 148 |
<?php |
| 149 |
printf( |
| 150 |
/* translators: %s is replaced with the link to the BeyondWords dashboard */ |
| 151 |
esc_html__('Listen to content saved as “Pending” in the %s.', 'speechkit'), |
| 152 |
sprintf( |
| 153 |
'<a href="%s" target="_blank" rel="nofollow">%s</a>', |
| 154 |
esc_url($projectUrl), |
| 155 |
esc_html__('BeyondWords dashboard', 'speechkit') |
| 156 |
) |
| 157 |
); |
| 158 |
?> |
| 159 |
</div> |
| 160 |
<?php |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Embed a player for a WordPress post. |
| 165 |
* |
| 166 |
* @param int|WP_Post (Optional) Post ID or WP_Post object. Default is global $post. |
| 167 |
*/ |
| 168 |
public function playerEmbed($post = null) |
| 169 |
{ |
| 170 |
$post = get_post($post); |
| 171 |
|
| 172 |
if (!($post instanceof \WP_Post)) { |
| 173 |
return; |
| 174 |
} |
| 175 |
?> |
| 176 |
<div id="beyondwords-metabox-player" data-post-id="<?php echo esc_attr($post->ID); ?>"> |
| 177 |
<div data-beyondwords-player="true" contenteditable="false"></div> |
| 178 |
</div> |
| 179 |
<?php |
| 180 |
} |
| 181 |
|
| 182 |
public function errors($post) |
| 183 |
{ |
| 184 |
$error = PostMetaUtils::getErrorMessage($post->ID); |
| 185 |
|
| 186 |
if ($error) : |
| 187 |
?> |
| 188 |
<div id="beyondwords-metabox-errors"> |
| 189 |
<div class="beyondwords-error"> |
| 190 |
<p> |
| 191 |
<?php echo esc_html($error); ?> |
| 192 |
</p> |
| 193 |
</div> |
| 194 |
<?php $this->regenerateInstructions(); ?> |
| 195 |
</div> |
| 196 |
<?php |
| 197 |
endif; |
| 198 |
} |
| 199 |
|
| 200 |
public function help() |
| 201 |
{ |
| 202 |
?> |
| 203 |
<p id="beyondwords-metabox-help"> |
| 204 |
<?php |
| 205 |
printf( |
| 206 |
/* translators: %s is replaced with the link to the support email address */ |
| 207 |
esc_html__('Need help? Email our support team on %s', 'speechkit'), |
| 208 |
sprintf('<a href="%s">%s</a>', 'mailto:support@beyondwords.io', 'support@beyondwords.io') |
| 209 |
); |
| 210 |
?> |
| 211 |
</p> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
public function regenerateInstructions() |
| 216 |
{ |
| 217 |
?> |
| 218 |
<!-- Update/regenerate --> |
| 219 |
<p> |
| 220 |
<?php |
| 221 |
_e('To create audio, resolve the error above then select ‘Update’ with ‘Generate audio’ checked.', 'speechkit'); // phpcs:ignore |
| 222 |
?> |
| 223 |
</p> |
| 224 |
<?php |
| 225 |
} |
| 226 |
} |
| 227 |
|