array( 'name' => __( 'Galleries', '3pika-gallery' ), 'singular_name' => __( 'Gallery', '3pika-gallery' ), ), 'public' => false, 'show_ui' => false, 'show_in_menu' => false, 'show_in_rest' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'rewrite' => false, 'query_var' => false, 'supports' => array( 'title', 'author' ), 'capability_type' => 'post', 'map_meta_cap' => true, ) ); } /** * Load full gallery data. * * @param int $id Gallery ID. * @return array|null */ public static function get( $id ) { $post = get_post( $id ); if ( ! $post || self::TYPE !== $post->post_type ) { return null; } $images = get_post_meta( $post->ID, '_tpika_images', true ); $filters = get_post_meta( $post->ID, '_tpika_filters', true ); $settings = get_post_meta( $post->ID, '_tpika_settings', true ); return array( 'id' => $post->ID, 'title' => $post->post_title, 'status' => $post->post_status, 'images' => is_array( $images ) ? array_values( $images ) : array(), 'filters' => is_array( $filters ) ? array_values( $filters ) : array(), 'settings' => wp_parse_args( is_array( $settings ) ? $settings : array(), TPIKA_Settings::get() ), ); } /** * Create or update a gallery. Input must already be sanitized. * * @param int $id 0 to create. * @param string $title Title. * @param array $images Images. * @param array $filters Filters. * @param array $settings Settings. * @return int|WP_Error */ public static function save( $id, $title, $images, $filters, $settings ) { $args = array( 'post_type' => self::TYPE, 'post_title' => $title ? $title : __( 'Untitled gallery', '3pika-gallery' ), 'post_status' => 'publish', ); if ( $id ) { $args['ID'] = $id; $result = wp_update_post( $args, true ); } else { $result = wp_insert_post( $args, true ); } if ( is_wp_error( $result ) ) { return $result; } $id = (int) $result; update_post_meta( $id, '_tpika_images', $images ); update_post_meta( $id, '_tpika_filters', $filters ); update_post_meta( $id, '_tpika_settings', $settings ); do_action( 'tpika_gallery_saved', $id ); return $id; } /** * Sanitize images list from the builder. * * @param array $images Raw images. * @param array $filter_ids Allowed filter IDs. * @return array */ public static function sanitize_images( $images, $filter_ids ) { $out = array(); if ( ! is_array( $images ) ) { return $out; } foreach ( $images as $img ) { if ( ! is_array( $img ) ) { continue; } $type = 'image'; $att = isset( $img['id'] ) ? absint( $img['id'] ) : 0; if ( $att && 'attachment' !== get_post_type( $att ) ) { $att = 0; } $item = array( 'type' => $type, 'id' => $att, 'caption' => isset( $img['caption'] ) ? sanitize_textarea_field( $img['caption'] ) : '', 'alt' => isset( $img['alt'] ) ? sanitize_text_field( $img['alt'] ) : '', 'link' => isset( $img['link'] ) ? esc_url_raw( $img['link'] ) : '', 'filters' => array(), ); $valid = (bool) $att; if ( ! $valid ) { continue; } if ( isset( $img['filters'] ) && is_array( $img['filters'] ) ) { foreach ( $img['filters'] as $f ) { $f = sanitize_key( $f ); if ( in_array( $f, $filter_ids, true ) ) { $item['filters'][] = $f; } } } $out[] = $item; } return $out; } /** * Sanitize filters list. * * @param array $filters Raw filters. * @return array */ public static function sanitize_filters( $filters ) { $out = array(); $seen = array(); if ( ! is_array( $filters ) ) { return $out; } foreach ( $filters as $f ) { if ( ! is_array( $f ) ) { continue; } $id = isset( $f['id'] ) ? sanitize_key( $f['id'] ) : ''; $name = isset( $f['name'] ) ? sanitize_text_field( $f['name'] ) : ''; if ( ! $id || '' === $name || isset( $seen[ $id ] ) ) { continue; } $seen[ $id ] = true; $color = isset( $f['color'] ) ? sanitize_hex_color( $f['color'] ) : ''; $text = isset( $f['text'] ) ? sanitize_hex_color( $f['text'] ) : ''; $out[] = array( 'id' => $id, 'name' => $name, 'color' => $color ? $color : '', 'text' => $text ? $text : '', ); } return $out; } /** * Add thumbnail URLs etc. for the admin builder. * * @param array $images Images. * @return array */ public static function images_for_admin( $images ) { foreach ( $images as &$img ) { $thumb = ''; $file = ''; if ( ! empty( $img['id'] ) ) { $src = wp_get_attachment_image_src( $img['id'], 'medium' ); $thumb = $src ? $src[0] : ''; $path = get_attached_file( (int) $img['id'] ); $file = $path ? wp_basename( $path ) : ''; } $img['thumb'] = $thumb; $img['file'] = $file; } return $images; } /** * Number of items in a gallery (cheap). * * @param int $id Gallery ID. * @return int */ public static function count( $id ) { $images = get_post_meta( $id, '_tpika_images', true ); return is_array( $images ) ? count( $images ) : 0; } }