PluginProbe
BeyondWords – AI audio for publishers / 4.2.4
BeyondWords – AI audio for publishers v4.2.4
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.2.4, at src/Component/Post/PlayerStyle/PlayerStyle.php

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