array( 'name' => __( 'Galleries', 'equaframe' ), 'singular_name' => __( 'Gallery', 'equaframe' ), ), 'public' => false, 'show_ui' => false, 'show_in_menu' => false, 'show_in_rest' => false, 'exclude_from_search' => true, 'rewrite' => false, 'query_var' => false, 'supports' => array( 'title' ), ) ); } /** * @return int|WP_Error New gallery ID. */ public static function create( $name, $layout ) { $id = wp_insert_post( wp_slash( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'post_title' => $name, ) ), true ); if ( is_wp_error( $id ) ) { return $id; } $settings = EquaFrame_Settings::defaults(); if ( isset( EquaFrame_Settings::layouts()[ $layout ] ) ) { $settings['layout'] = $layout; } update_post_meta( $id, self::META_SETTINGS, wp_slash( $settings ) ); update_post_meta( $id, self::META_IMAGES, array() ); return (int) $id; } public static function get( $id ) { $post = get_post( (int) $id ); if ( ! $post || self::POST_TYPE !== $post->post_type || 'trash' === $post->post_status ) { return null; } return $post; } public static function all() { return get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', ) ); } public static function get_settings( $id ) { return EquaFrame_Settings::with_defaults( get_post_meta( (int) $id, self::META_SETTINGS, true ) ); } /** * @param int $id Gallery ID. * @param array $input Unslashed submitted settings. * @param string $name New gallery name (optional). */ public static function save_settings( $id, $input, $name = '' ) { $settings = EquaFrame_Settings::sanitize( $input, self::get_settings( $id ) ); update_post_meta( $id, self::META_SETTINGS, wp_slash( $settings ) ); if ( '' !== $name ) { wp_update_post( wp_slash( array( 'ID' => $id, 'post_title' => $name ) ) ); } return $settings; } public static function get_images( $id ) { $images = get_post_meta( (int) $id, self::META_IMAGES, true ); return is_array( $images ) ? array_values( $images ) : array(); } /** * Clean a list of images from the admin UI and keep only real image attachments. * * @param mixed $raw Decoded JSON (already unslashed). */ public static function sanitize_images( $raw ) { $images = array(); if ( ! is_array( $raw ) ) { return $images; } foreach ( $raw as $row ) { if ( ! is_array( $row ) ) { continue; } // Only positive whole numbers are attachment IDs; "-6" must not turn into 6. $attachment_id = ( isset( $row['id'] ) && is_scalar( $row['id'] ) ) ? (int) $row['id'] : 0; if ( $attachment_id < 1 || ! wp_attachment_is_image( $attachment_id ) ) { continue; } $images[] = array( 'id' => $attachment_id, 'title' => self::clean_text( isset( $row['title'] ) ? $row['title'] : '' ), 'alt' => self::clean_text( isset( $row['alt'] ) ? $row['alt'] : '' ), 'caption' => self::clean_text( isset( $row['caption'] ) ? $row['caption'] : '' ), 'css_id' => isset( $row['css_id'] ) ? self::sanitize_css_id( $row['css_id'] ) : '', ); } return $images; } /** * Single-line text from the admin UI, capped at a sensible length. * * @param mixed $value Raw value. * @return string */ private static function clean_text( $value ) { return mb_substr( sanitize_text_field( is_scalar( $value ) ? (string) $value : '' ), 0, 1000 ); } /** * HTML IDs must not contain spaces and are easiest to target in CSS when they * start with a letter. */ public static function sanitize_css_id( $value ) { $value = preg_replace( '/[^A-Za-z0-9_\-:.]/', '', (string) $value ); if ( '' !== $value && ! preg_match( '/^[A-Za-z]/', $value ) ) { $value = 'id-' . $value; } return substr( $value, 0, 80 ); } public static function save_images( $id, $images ) { update_post_meta( $id, self::META_IMAGES, wp_slash( $images ) ); wp_update_post( array( 'ID' => $id ) ); // Touch the modified date. } public static function duplicate( $id ) { $post = self::get( $id ); if ( ! $post ) { return 0; } /* translators: %s: gallery name */ $title = sprintf( __( '%s (copy)', 'equaframe' ), $post->post_title ); $new = wp_insert_post( wp_slash( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'post_title' => $title, ) ) ); if ( ! $new || is_wp_error( $new ) ) { return 0; } update_post_meta( $new, self::META_SETTINGS, wp_slash( self::get_settings( $id ) ) ); update_post_meta( $new, self::META_IMAGES, wp_slash( self::get_images( $id ) ) ); return (int) $new; } public static function delete( $id ) { return self::get( $id ) ? (bool) wp_delete_post( $id, true ) : false; } }