PluginProbe
BeyondWords – AI audio for publishers / 4.7.0
BeyondWords – AI audio for publishers v4.7.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Post / PlayerStyle / PlayerStyle.php

PlayerStyle.php in BeyondWords – AI audio for publishers 4.7.0, at src/Component/Post/PlayerStyle/PlayerStyle.php

168 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * BeyondWords Component: Player style
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <stu@beyondwords.io>
10 * @since 4.1.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Post\PlayerStyle;
14
15 use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
16 use Beyondwords\Wordpress\Component\Settings\PlayerStyle\PlayerStyle as PlayerStyleSetting;
17 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
18
19 /**
20 * PlayerStyle setup
21 *
22 * @since 4.1.0
23 */
24 class PlayerStyle
25 {
26 /**
27 * Constructor
28 */
29 public function init()
30 {
31 add_action('rest_api_init', array($this, 'restApiInit'));
32
33 add_action('wp_loaded', function () {
34 $postTypes = SettingsUtils::getCompatiblePostTypes();
35
36 if (is_array($postTypes)) {
37 foreach ($postTypes as $postType) {
38 add_action("save_post_{$postType}", array($this, 'save'), 10);
39 }
40 }
41 });
42 }
43
44 /**
45 * HTML output for this component.
46 *
47 * @since 4.1.0
48 * @since 4.5.1 Hide element if no language data exists.
49 *
50 * @param WP_Post $post The post object.
51 *
52 * @return string|null
53 */
54 public function element($post)
55 {
56 $projectId = PostMetaUtils::getProjectId($post->ID);
57
58 $playerStyle = PostMetaUtils::getPlayerStyle($post->ID);
59 $allPlayerStyles = PlayerStyleSetting::getCachedPlayerStyles($projectId);
60
61 if (! is_array($allPlayerStyles) || ! count($allPlayerStyles)) {
62 return;
63 }
64
65 wp_nonce_field('beyondwords_player_style', 'beyondwords_player_style_nonce');
66 ?>
67 <p
68 id="beyondwords-metabox-player-style"
69 class="post-attributes-label-wrapper page-template-label-wrapper"
70 >
71 <label class="post-attributes-label" for="beyondwords_player_style">
72 <?php esc_html_e('Player style', 'speechkit'); ?>
73 </label>
74 </p>
75 <select id="beyondwords_player_style" name="beyondwords_player_style" style="width: 100%;">
76 <?php
77 foreach ($allPlayerStyles as $item) {
78 printf(
79 '<option value="%s" %s %s>%s</option>',
80 esc_attr($item['value']),
81 selected(strval($item['value']), $playerStyle),
82 disabled($item['disabled'] ?? false, true),
83 esc_html($item['label'])
84 );
85 }
86 ?>
87 </select>
88 <?php
89 }
90
91 /**
92 * Save the meta when the post is saved.
93 *
94 * @since 4.1.0
95 *
96 * @param int $postId The ID of the post being saved.
97 */
98 public function save($postId)
99 {
100 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
101 return $postId;
102 }
103
104 // "save_post" can be triggered at other times, so verify this request came from the our component
105 if (! isset($_POST['beyondwords_player_style']) || ! isset($_POST['beyondwords_player_style_nonce'])) {
106 return $postId;
107 }
108
109 // "save_post" can be triggered at other times, so verify this request came from the our component
110 if (
111 ! wp_verify_nonce(
112 sanitize_text_field($_POST['beyondwords_player_style_nonce']),
113 'beyondwords_player_style'
114 )
115 ) {
116 return $postId;
117 }
118
119 $playerStyle = sanitize_text_field($_POST['beyondwords_player_style']);
120
121 if (! empty($playerStyle)) {
122 update_post_meta($postId, 'beyondwords_player_style', $playerStyle);
123 } else {
124 delete_post_meta($postId, 'beyondwords_player_style');
125 }
126
127 return $postId;
128 }
129
130 /**
131 * Register WP REST API route
132 *
133 * @since 4.1.0
134 *
135 * @return void
136 */
137 public function restApiInit()
138 {
139 // Player styles endpoint
140 register_rest_route('beyondwords/v1', '/projects/(?P<projectId>[0-9]+)/player-styles', array(
141 'methods' => \WP_REST_Server::READABLE,
142 'callback' => array($this, 'playerStylesRestApiResponse'),
143 'permission_callback' => function () {
144 return current_user_can('edit_posts');
145 },
146 ));
147 }
148
149 /**
150 * "Player styles" WP REST API response (required for the Gutenberg editor).
151 *
152 * @since 4.1.0
153 *
154 * @return \WP_REST_Response
155 */
156 public function playerStylesRestApiResponse(\WP_REST_Request $data)
157 {
158 $params = $data->get_url_params();
159
160 $response = PlayerStyleSetting::getCachedPlayerStyles($params['projectId']);
161
162 // Convert from object to array so we can use find() in Block Editor JS.
163 $response = array_values($response);
164
165 return new \WP_REST_Response($response);
166 }
167 }
168