false, 'labels' => array( 'name' => __( 'Video Playlists', 'videopress-hq' ), 'singular_name' => __( 'Video Playlist', 'videopress-hq' ), 'search_items' => __( 'Search Playlists', 'videopress-hq' ), 'all_items' => __( 'All Playlists', 'videopress-hq' ), 'edit_item' => __( 'Edit Playlist', 'videopress-hq' ), 'update_item' => __( 'Update Playlist', 'videopress-hq' ), 'add_new_item' => __( 'Add New Playlist', 'videopress-hq' ), 'new_item_name' => __( 'New Playlist Name', 'videopress-hq' ), 'menu_name' => __( 'Playlists', 'videopress-hq' ), ), 'show_ui' => true, 'show_in_rest' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'playlist' ), ) ); } public static function comment_reply_link( $link, $args, $comment, $post ) { if ( ! $post || 'vp_video' !== $post->post_type ) { return $link; } $playlist_slug = get_query_var( self::TAXONOMY_NAME ); if ( ! $playlist_slug ) { return $link; } $playlist_slug = sanitize_text_field( $playlist_slug ); $playlist = self::get_by_slug( $playlist_slug ); if ( ! $playlist ) { return $link; } if ( ! preg_match( '/href="([^"]*)"/', $link, $matches ) && ! preg_match( "/href='([^']*)'/", $link, $matches ) ) { return $link; } $query = parse_url( $matches[1] ); if ( ! $query ) { return $link; } $url = add_query_arg( self::TAXONOMY_NAME, $playlist->term->slug, $matches[1] ); return str_replace( $matches[1], esc_attr( $url ), $link ); } public static function comment_post_redirect( $location, $comment ) { $referrer = wp_get_referer(); if ( ! $referrer ) { return $location; } $referrer_query = parse_url( $referrer, PHP_URL_QUERY ); if ( ! $referrer_query ) { return $location; } $referrer_query = wp_parse_args( $referrer_query ); if ( ! isset( $referrer_query[ self::TAXONOMY_NAME ] ) ) { return $location; } $playlist_slug = sanitize_text_field( $referrer_query[ self::TAXONOMY_NAME ] ); $playlist = self::get_by_slug( $playlist_slug ); if ( ! $playlist ) { return $location; } return add_query_arg( self::TAXONOMY_NAME, $playlist->term->slug, $location ); } public static function is_valid_visibility( $visibility ) { return in_array( $visibility, array( self::VISIBILITY_PUBLIC, self::VISIBILITY_PROTECTED, self::VISIBILITY_UNPUBLISHED, ), true ); } public static function delete_term_relationships( $object_id, $tt_ids, $taxonomy ) { if ( self::TAXONOMY_NAME !== $taxonomy ) { return; } $post = get_post( $object_id ); if ( ! $post || 'vp_video' !== $post->post_type ) { return; } foreach ( $tt_ids as $term_id ) { delete_post_meta( $object_id, self::get_order_meta_key( $term_id ) ); } } public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { if ( self::TAXONOMY_NAME !== $taxonomy ) { return; } $post = get_post( $object_id ); if ( ! $post || 'vp_video' !== $post->post_type ) { return; } $diff_add = array_diff( $tt_ids, $old_tt_ids ); $diff_rm = array_diff( $old_tt_ids, $tt_ids ); if ( empty( $diff_add ) && empty( $diff_rm ) ) { return; } if ( ! $append && ! empty( $diff_rm ) ) { foreach ( $diff_rm as $term_id ) { delete_post_meta( $object_id, self::get_order_meta_key( $term_id ) ); } } if ( ! empty( $diff_add ) ) { $terms_to_add = get_terms( array( 'include' => $diff_add ) ); if ( empty( $terms_to_add ) || is_wp_error( $terms_to_add ) ) { return; } foreach ( $terms_to_add as $term_id ) { $term = get_term( $term_id, self::TAXONOMY_NAME ); if ( ! $term || is_wp_error( $term ) ) { continue; } update_post_meta( $object_id, self::get_order_meta_key( $term_id ), $term->count + 1 ); } } } public static function from_terms( array $terms ) { $result = array(); if ( empty( $terms ) ) { return $result; } /** * @var WP_Term $term */ foreach ( $terms as $term ) { $instance = self::from_term( $term ); if ( null === $instance ) { continue; } $result[] = $instance; } return $result; } public static function from_term( $term ) { if ( ! $term || self::TAXONOMY_NAME !== $term->taxonomy ) { return null; } $instance = new VideoPressHQPlaylist(); $instance->term = $term; return $instance; } public static function get_by_slug( $slug ) { $term = get_term_by( 'slug', $slug, self::TAXONOMY_NAME ); if ( ! $term ) { return null; } return self::from_term( $term ); } public static function get_order_meta_key( $term_id ) { return self::META_ITEMS_ORDER . '-' . $term_id; } public function is_visible_to_current_user( $include_unpublished = true ) { if ( self::VISIBILITY_PUBLIC === $this->term->term_group ) { return true; } if ( ! is_user_logged_in() ) { return false; } if ( self::VISIBILITY_PROTECTED === $this->term->term_group ) { return true; } return $include_unpublished && current_user_can( VideoPressHQCaps::CAP_READ_PRIVATE_PLAYLISTS ); } public function set_items( array $items ) { self::unregister_hooks(); $all_items_query = $this->get_posts_query( array( 'post_status' => 'any', 'fields' => 'ids', ) ); $all_item_ids = $all_items_query->get_posts(); foreach ( $all_item_ids as $item_id ) { wp_remove_object_terms( $item_id, $this->term->term_id, self::TAXONOMY_NAME ); delete_post_meta( $item_id, self::get_order_meta_key( $this->term->term_id ) ); } $order = 1; foreach ( $items as $item_id ) { if ( ! is_numeric( $item_id ) ) { continue; } wp_add_object_terms( $item_id, $this->term->term_id, self::TAXONOMY_NAME ); update_post_meta( $item_id, self::get_order_meta_key( $this->term->term_id ), $order ); $order++; } self::register_hooks(); } /** * @return array|WP_Error|bool */ public function update() { if ( ! $this->term ) { return false; } $update_result = wp_update_term( $this->term->term_id, self::TAXONOMY_NAME, array( 'name' => $this->term->name, 'slug' => $this->term->slug, 'description' => $this->term->description, 'term_group' => $this->term->term_group ?? self::VISIBILITY_UNPUBLISHED, ) ); return is_wp_error( $update_result ) ? $update_result : true; } public static function create( $name, $description = '' ) { $term = wp_insert_term( $name, self::TAXONOMY_NAME, array( 'description' => $description, ) ); if ( is_wp_error( $term ) ) { return $term; } $term = get_term( $term['term_id'], self::TAXONOMY_NAME ); return self::from_term( $term ); } public function get_post_permalink( $post_id ) { $permalink = get_permalink( $post_id ); if ( ! $permalink ) { return ''; } $permalink = add_query_arg( self::TAXONOMY_NAME, $this->term->slug, $permalink ); return $permalink; } public function get_posts_query_params() { $order_meta_key = self::get_order_meta_key( $this->term->term_id ); return array( 'post_type' => 'vp_video', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array( 'key' => $order_meta_key, 'compare' => 'EXISTS', ), array( 'key' => $order_meta_key, 'compare' => 'NOT EXISTS', ), ), 'orderby' => 'meta_value_num', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => self::TAXONOMY_NAME, 'field' => 'term_id', 'terms' => $this->term->term_id, ), ), ); } public function get_posts_query( array $query_params = array() ) { $query_params += $this->get_posts_query_params(); return new WP_Query( $query_params ); } public static function delete( $term ) { if ( ! $term || self::TAXONOMY_NAME !== $term->taxonomy ) { return new WP_Error( 'invalid_term', __( 'Invalid playlist', 'videopress-hq' ) ); } return wp_delete_term( $term->term_id, self::TAXONOMY_NAME ); } }