args (type, default, schema, sanitize). * * @return array */ abstract protected function meta(); /** * Hook in. */ public function register() { add_action( 'init', array( $this, 'register_post_type' ) ); add_action( 'init', array( $this, 'register_meta' ) ); } /** * Post type supports. * * @return array */ protected function supports() { return array( 'title', 'editor', 'thumbnail', 'custom-fields' ); } /** * Extra register_post_type() args. * * @return array */ protected function args() { return array(); } /** * Register CPT. */ public function register_post_type() { list( $plural, $singular ) = $this->labels(); register_post_type( static::POST_TYPE, array_merge( array( 'labels' => array( 'name' => $plural, 'singular_name' => $singular, ), 'public' => false, 'show_ui' => false, 'show_in_rest' => true, 'rest_base' => $this->rest_base(), 'rest_namespace' => 'wp/v2', 'supports' => $this->supports(), 'capability_type' => 'post', 'map_meta_cap' => true, 'exclude_from_search' => true, ), $this->args() ) ); } /** * Sanitize a social-links map: { network => URL }. Network names become keys, URLs are * validated, empty entries are dropped. * * @param mixed $social Raw value. * @return array */ public static function sanitize_social_links( $social ) { $out = array(); if ( ! is_array( $social ) ) { return $out; } foreach ( $social as $key => $url ) { $key = sanitize_key( $key ); $url = is_scalar( $url ) ? esc_url_raw( trim( (string) $url ), array( 'http', 'https' ) ) : ''; $host = $url ? (string) wp_parse_url( $url, PHP_URL_HOST ) : ''; if ( $key && $url && false !== strpos( $host, '.' ) ) { $out[ $key ] = $url; } } return $out; } /** * Register meta. */ public function register_meta() { foreach ( $this->meta() as $key => $def ) { $args = array( 'type' => isset( $def['type'] ) ? $def['type'] : 'string', 'single' => true, 'show_in_rest' => isset( $def['schema'] ) ? array( 'schema' => $def['schema'] ) : true, 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, ); if ( isset( $def['default'] ) ) { $args['default'] = $def['default']; } if ( isset( $def['sanitize'] ) ) { $args['sanitize_callback'] = $def['sanitize']; } register_post_meta( static::POST_TYPE, '_streamsera_' . $key, $args ); } } }