array( 'name' => __( 'Players', 'streamsera' ), 'singular_name' => __( 'Player', 'streamsera' ), ), 'public' => false, 'show_ui' => false, 'show_in_rest' => true, 'rest_base' => 'streamsera-players', 'rest_namespace' => 'wp/v2', 'supports' => array( 'title', 'custom-fields' ), 'capability_type' => 'post', 'map_meta_cap' => true, 'exclude_from_search' => true, ) ); } /** * Register the config meta (free-form object, sanitized on save). */ public function register_meta() { register_post_meta( self::POST_TYPE, '_streamsera_config', array( 'type' => 'object', 'single' => true, 'default' => streamsera_default_player_config(), 'show_in_rest' => array( 'schema' => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => true, ), ), 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'sanitize_callback' => 'streamsera_sanitize_player_config', ) ); } /** * Make the first published station of a player its primary one. * * When the primary station is deleted or unpublished, the next published station of the player * takes its place (and leaves the extra-stations list), as in the builder preview, so the player * keeps working on the site. * * @param array $config Player config. * @return array */ public static function with_live_station( array $config ) { $primary = isset( $config['station'] ) ? (int) $config['station'] : 0; if ( $primary && Station::to_public( $primary ) ) { return $config; } $extra = isset( $config['stations'] ) ? array_values( (array) $config['stations'] ) : array(); foreach ( $extra as $i => $sid ) { if ( Station::to_public( (int) $sid ) ) { $config['station'] = (int) $sid; unset( $extra[ $i ] ); $config['stations'] = array_values( $extra ); break; } } return $config; } /** * Get a merged config for a player. * * @param int $post_id Player ID. * @param bool $include_drafts Also resolve draft/pending players (admin preview only). * @return array|null */ public static function get_config( $post_id, $include_drafts = false ) { $post = get_post( $post_id ); if ( ! $post || self::POST_TYPE !== $post->post_type ) { return null; } if ( 'publish' !== $post->post_status && ( ! $include_drafts || ! in_array( $post->post_status, array( 'draft', 'pending', 'private', 'future' ), true ) ) ) { return null; } $config = get_post_meta( $post_id, '_streamsera_config', true ); $config = streamsera_sanitize_player_config( is_array( $config ) ? $config : array() ); $config['id'] = $post_id; /** * Filter the resolved player config. * * @param array $config Config. * @param int $post_id Player ID. */ return apply_filters( 'streamsera_player_config', $config, $post_id ); } }