API
2 years ago
Blocks
5 years ago
License
5 years ago
AdminNotice.php
5 years ago
AdminNotices.php
4 years ago
AjaxActions.php
2 years ago
Blocks.php
5 years ago
Compatibility.php
4 years ago
Menu.php
3 years ago
Migrations.php
5 years ago
Player.php
2 years ago
ProCompatibility.php
2 years ago
ReusableVideos.php
4 years ago
Scripts.php
2 years ago
Settings.php
4 years ago
Shortcodes.php
2 years ago
Streamer.php
4 years ago
Translation.php
2 years ago
VideoPostType.php
4 years ago
VideoPostType.php
305 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Services; |
| 4 | |
| 5 | class VideoPostType |
| 6 | { |
| 7 | protected $post_type = 'pp_video_block'; |
| 8 | |
| 9 | public function register() |
| 10 | { |
| 11 | global $wp_version; |
| 12 | |
| 13 | add_action('init', [$this, 'init']); |
| 14 | |
| 15 | if (version_compare($wp_version, '5.8', ">=")) { |
| 16 | add_filter("allowed_block_types_all", [$this, 'allowedTypes'], 10, 2); |
| 17 | } else { |
| 18 | add_filter("allowed_block_types", [$this, 'allowedTypesDeprecated'], 10, 2); |
| 19 | } |
| 20 | |
| 21 | add_filter('enter_title_here', [$this, 'videoTitle']); |
| 22 | |
| 23 | // post type ui |
| 24 | add_filter("manage_{$this->post_type}_posts_columns", [$this, 'postTypeColumns'], 1); |
| 25 | add_action("manage_{$this->post_type}_posts_custom_column", [$this, 'postTypeContent'], 10, 2); |
| 26 | |
| 27 | // filter by tags |
| 28 | add_action('restrict_manage_posts', [$this, 'tagFilter']); |
| 29 | add_action('parse_query', [$this, 'tagQuery']); |
| 30 | |
| 31 | // force gutenberg here |
| 32 | add_action('use_block_editor_for_post', [$this, 'forceGutenberg'], 999, 2); |
| 33 | |
| 34 | // limit media hub posts |
| 35 | add_filter('pre_get_posts', [$this, 'limitMediaHubPosts']); |
| 36 | |
| 37 | add_action( 'transition_post_status', [$this, 'set_title_on_publish_only'], 10, 3 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Limit media hub posts by author if cannot edit others posts |
| 42 | * |
| 43 | * @param \WP_Query $query |
| 44 | * @return \WP_Query |
| 45 | */ |
| 46 | public function limitMediaHubPosts($query) |
| 47 | { |
| 48 | global $pagenow, $typenow; |
| 49 | |
| 50 | if ('edit.php' != $pagenow || !$query->is_admin || 'pp_video_block' !== $typenow) { |
| 51 | return $query; |
| 52 | } |
| 53 | |
| 54 | if (!current_user_can('edit_others_posts')) { |
| 55 | $query->set('author', get_current_user_id()); |
| 56 | } |
| 57 | |
| 58 | return $query; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Force gutenberg in case of classic editor |
| 63 | */ |
| 64 | public function forceGutenberg($use, $post) |
| 65 | { |
| 66 | if ($this->post_type === $post->post_type) { |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | return $use; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Columns on all posts page |
| 75 | * |
| 76 | * @param array $defaults |
| 77 | * @return array |
| 78 | */ |
| 79 | public function postTypeColumns($defaults) |
| 80 | { |
| 81 | $columns = array_merge($defaults, array( |
| 82 | 'title' => $defaults['title'], |
| 83 | 'shortcode' => __('Shortcode', 'presto-player'), |
| 84 | 'php_function' => __('PHP Function', 'presto-player'), |
| 85 | )); |
| 86 | |
| 87 | $v = $columns['taxonomy-pp_video_tag']; |
| 88 | unset($columns['taxonomy-pp_video_tag']); |
| 89 | $columns['taxonomy-pp_video_tag'] = $v; |
| 90 | |
| 91 | $v = $columns['date']; |
| 92 | unset($columns['date']); |
| 93 | $columns['date'] = $v; |
| 94 | return $columns; |
| 95 | } |
| 96 | |
| 97 | public function postTypeContent($column_name, $post_ID) |
| 98 | { |
| 99 | if ('shortcode' === $column_name) { |
| 100 | echo '<code>[presto_player id=' . (int) $post_ID . ']</code>'; |
| 101 | } |
| 102 | if ('php_function' === $column_name) { |
| 103 | echo '<code>presto_player(' . (int) $post_ID . ')</code>'; |
| 104 | } |
| 105 | if ('video_tags' === $column_name) { |
| 106 | $tags = get_the_terms($post_ID, 'pp_video_tag'); |
| 107 | if (is_array($tags)) { |
| 108 | foreach ($tags as $key => $tag) { |
| 109 | $tags[$key] = '<a href="?post_type=pp_video_block&pp_video_tag=' . $tag->term_id . '">' . $tag->name . '</a>'; |
| 110 | } |
| 111 | echo implode(', ', $tags); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public function videoTitle($title) |
| 117 | { |
| 118 | $screen = get_current_screen(); |
| 119 | if ($this->post_type == $screen->post_type) { |
| 120 | $title = __('Enter a title...', 'presto-player'); |
| 121 | } |
| 122 | return $title; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Allowed block types |
| 127 | * |
| 128 | * @param array $allowed_block_types |
| 129 | * @param object $block_editor_content |
| 130 | * @return void |
| 131 | */ |
| 132 | public function allowedTypes($allowed_block_types, $block_editor_content) |
| 133 | { |
| 134 | if (!empty($block_editor_content->post->post_type)) { |
| 135 | if ($block_editor_content->post->post_type === $this->post_type) { |
| 136 | return [ |
| 137 | 'presto-player/reusable', |
| 138 | 'presto-player/self-hosted', |
| 139 | 'presto-player/youtube', |
| 140 | 'presto-player/vimeo', |
| 141 | 'presto-player/bunny', |
| 142 | 'presto-player/audio' |
| 143 | ]; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $allowed_block_types; |
| 148 | } |
| 149 | |
| 150 | public function allowedTypesDeprecated($allowed_block_types, $post) |
| 151 | { |
| 152 | if ($post->post_type !== $this->post_type) { |
| 153 | return $allowed_block_types; |
| 154 | } |
| 155 | |
| 156 | return [ |
| 157 | 'presto-player/reusable', |
| 158 | 'presto-player/self-hosted', |
| 159 | 'presto-player/youtube', |
| 160 | 'presto-player/vimeo', |
| 161 | 'presto-player/bunny', |
| 162 | 'presto-player/audio' |
| 163 | ]; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Register post type |
| 168 | * |
| 169 | * @return void |
| 170 | */ |
| 171 | public function init() |
| 172 | { |
| 173 | register_taxonomy('pp_video_tag', 'pp_video_block', [ |
| 174 | 'labels' => array( |
| 175 | 'name' => _x('Media Tags', 'post type general name'), |
| 176 | 'singular_name' => _x('Media Tag', 'post type singular name'), |
| 177 | 'search_items' => _x('Search Media Tags', 'admin menu'), |
| 178 | 'popular_items' => _x('Popular Media Tags', 'add new on admin bar'), |
| 179 | ), |
| 180 | 'label' => __('Tag', 'presto-player'), |
| 181 | 'public' => false, |
| 182 | 'show_ui' => true, |
| 183 | 'show_in_rest' => true, |
| 184 | 'show_admin_column' => true, |
| 185 | ]); |
| 186 | |
| 187 | register_post_type( |
| 188 | 'pp_video_block', |
| 189 | array( |
| 190 | 'labels' => array( |
| 191 | 'name' => _x('Media Hub', 'post type general name', 'presto-player'), |
| 192 | 'singular_name' => _x('Media', 'post type singular name', 'presto-player'), |
| 193 | 'menu_name' => _x('Media', 'admin menu', 'presto-player'), |
| 194 | 'name_admin_bar' => _x('Video', 'add new on admin bar', 'presto-player'), |
| 195 | 'add_new' => _x('Add New', 'Video', 'presto-player'), |
| 196 | 'add_new_item' => __('Add New Video', 'presto-player'), |
| 197 | 'new_item' => __('New Video', 'presto-player'), |
| 198 | 'edit_item' => __('Edit Video', 'presto-player'), |
| 199 | 'view_item' => __('View Video', 'presto-player'), |
| 200 | 'all_items' => __('All Videos', 'presto-player'), |
| 201 | 'search_items' => __('Search Media', 'presto-player'), |
| 202 | 'not_found' => __('No Videos found.', 'presto-player'), |
| 203 | 'not_found_in_trash' => __('No Videos found in Trash.', 'presto-player'), |
| 204 | 'filter_items_list' => __('Filter Videos list', 'presto-player'), |
| 205 | 'items_list_navigation' => __('Videos list navigation', 'presto-player'), |
| 206 | 'items_list' => __('Videos list', 'presto-player'), |
| 207 | 'item_published' => __('Video published.', 'presto-player'), |
| 208 | 'item_published_privately' => __('Video published privately.', 'presto-player'), |
| 209 | 'item_reverted_to_draft' => __('Video reverted to draft.', 'presto-player'), |
| 210 | 'item_scheduled' => __('Video scheduled.', 'presto-player'), |
| 211 | 'item_updated' => __('Video updated.', 'presto-player'), |
| 212 | ), |
| 213 | 'public' => false, |
| 214 | 'show_ui' => true, |
| 215 | 'show_in_menu' => false, |
| 216 | 'rewrite' => false, |
| 217 | 'show_in_rest' => true, |
| 218 | 'rest_base' => 'presto-videos', |
| 219 | 'rest_controller_class' => 'WP_REST_Blocks_Controller', |
| 220 | 'map_meta_cap' => true, |
| 221 | 'supports' => [ |
| 222 | 'title', |
| 223 | 'editor', |
| 224 | ], |
| 225 | 'taxonomies' => ['pp_video_tag'], |
| 226 | 'template' => [ |
| 227 | ['presto-player/reusable-edit'] |
| 228 | ], |
| 229 | 'template_lock' => 'all' |
| 230 | ) |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Adds a tag filter dropdown |
| 236 | * |
| 237 | * @return void |
| 238 | */ |
| 239 | public function tagFilter() |
| 240 | { |
| 241 | global $typenow; |
| 242 | |
| 243 | $post_type = 'pp_video_block'; |
| 244 | $taxonomy = 'pp_video_tag'; |
| 245 | |
| 246 | if ($typenow !== $post_type) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; |
| 251 | $info_taxonomy = get_taxonomy($taxonomy); |
| 252 | |
| 253 | wp_dropdown_categories(array( |
| 254 | 'show_option_all' => sprintf(__('Show all %s', 'textdomain'), $info_taxonomy->label), |
| 255 | 'taxonomy' => $taxonomy, |
| 256 | 'name' => $taxonomy, |
| 257 | 'orderby' => 'name', |
| 258 | 'selected' => $selected, |
| 259 | 'show_count' => true, |
| 260 | 'hide_empty' => true, |
| 261 | )); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Modify admin query for tag |
| 266 | * |
| 267 | * @param \WP_Query $query |
| 268 | * @return void |
| 269 | */ |
| 270 | public function tagQuery($query) |
| 271 | { |
| 272 | global $pagenow; |
| 273 | |
| 274 | $post_type = 'pp_video_block'; |
| 275 | $taxonomy = 'pp_video_tag'; |
| 276 | |
| 277 | $q_vars = &$query->query_vars; |
| 278 | if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) { |
| 279 | $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); |
| 280 | $q_vars[$taxonomy] = $term->slug; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Set media hub video title when kept empty before publish. |
| 286 | */ |
| 287 | public function set_title_on_publish_only( $new_status, $old_status, $post ) { |
| 288 | if ( ( 'publish' === $new_status && 'publish' !== $old_status ) |
| 289 | && 'pp_video_block' === $post->post_type |
| 290 | ) { |
| 291 | |
| 292 | if( empty( $post->post_title ) ) { |
| 293 | $new_title = "Presto Player #" . $post->ID; |
| 294 | |
| 295 | $post_update = array( |
| 296 | 'ID' => $post->ID, |
| 297 | 'post_title' => $new_title |
| 298 | ); |
| 299 | |
| 300 | wp_update_post( $post_update ); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 |