| @@ -4,8 +4,15 @@ | ||
| 4 | 4 | { |
| 5 | 5 | private $core; |
| 6 | 6 | private $namespace = 'meow-gallery/v1'; |
| 7 | 7 | |
| 8 | + // Gallery attributes that decide *which* media a gallery shows. They must never be taken from | |
| 9 | + // an untrusted request: see rest_load_gallery_collection(). | |
| 10 | + private static $source_atts = [ | |
| 11 | + 'collection', 'id', 'ids', 'include', 'tags', 'posts', 'latest_posts', 'attachments', | |
| 12 | + 'rml', 'wplr-collection', 'meow', | |
| 13 | + ]; | |
| 14 | + | |
| 8 | 15 | public function __construct( $core ) { |
| 9 | 16 | $this->core = $core; |
| 10 | 17 | |
| 11 | 18 | // FOR DEBUG |
| @@ -120,9 +127,9 @@ | ||
| 120 | 127 | |
| 121 | 128 | // Gallery |
| 122 | 129 | register_rest_route( $this->namespace, '/images/', array( |
| 123 | 130 | 'methods' => 'POST', |
| 124 | - 'permission_callback' => '__return_true', | |
| 131 | + 'permission_callback' => array( $this, 'can_load_images' ), | |
| 125 | 132 | 'callback' => array( $this, 'rest_images' ) |
| 126 | 133 | ) ); |
| 127 | 134 | |
| 128 | 135 | register_rest_route( $this->namespace, '/fetch_posts', array( |
| @@ -136,8 +143,18 @@ | ||
| 136 | 143 | ) |
| 137 | 144 | ) ); |
| 138 | 145 | } |
| 139 | 146 | |
| 147 | + // The /images/ route feeds the infinite scroll and nothing else: when it is off (the default, | |
| 148 | + // and always in the free version) the gallery is rendered whole and the front-end never calls | |
| 149 | + // this. It has to stay open to visitors when infinite scroll IS on, but leaving it open | |
| 150 | + // everywhere exposed the title, caption and URL of any attachment ID, including attachments of | |
| 151 | + // posts that are not published. | |
| 152 | + public function can_load_images() { | |
| 153 | + $infinite = class_exists( 'MeowPro_MGL_Core' ) && Meow_MGL_Core::get_plugin_option( 'infinite', false ); | |
| 154 | + return apply_filters( 'mgl_allow_load_images', (bool) $infinite ); | |
| 155 | + } | |
| 156 | + | |
| 140 | 157 | function preview( WP_REST_Request $request ) { |
| 141 | 158 | $params = $request->get_body( ); |
| 142 | 159 | $params = json_decode( $params ); |
| 143 | 160 | $params->ids = implode( ',', $params->ids ); |
| @@ -142,14 +159,20 @@ | ||
| 142 | 159 | $params = json_decode( $params ); |
| 143 | 160 | $params->ids = implode( ',', $params->ids ); |
| 144 | 161 | $atts = ( array ) $params; |
| 145 | 162 | |
| 163 | + $full = !empty( $atts['full'] ); | |
| 164 | + unset( $atts['full'] ); | |
| 165 | + | |
| 146 | 166 | $is_collection = isset( $atts['collection'] ) && !empty( $atts['collection'] ); |
| 147 | 167 | if ( $is_collection ) { |
| 148 | - $html = do_shortcode( '[meow-collection id="' . $atts['collection'] . '"]' ); | |
| 168 | + $html = $this->core->render_collection( $atts['collection'] ); | |
| 149 | 169 | $counts = [ 'total' => 0, 'shown' => 0 ]; |
| 150 | 170 | } else { |
| 151 | 171 | $this->core->last_preview_counts = [ 'total' => 0, 'shown' => 0 ]; |
| 172 | + if ( $full ) { | |
| 173 | + $this->core->preview_cutoff = PHP_INT_MAX; | |
| 174 | + } | |
| 152 | 175 | $html = $this->core->gallery( $atts, [ 'isPreview' => true ] ); |
| 153 | 176 | $counts = $this->core->last_preview_counts; |
| 154 | 177 | } |
| 155 | 178 | |
| @@ -164,11 +187,12 @@ | ||
| 164 | 187 | |
| 165 | 188 | function rest_load_gallery_collection( $request ) { |
| 166 | 189 | try { |
| 167 | 190 | $params = $request->get_json_params( ); |
| 168 | - $gallery_id = $params['id']; | |
| 169 | - $search_slug = $params['search_slug']; | |
| 170 | - $gallery_atts = $params['gallery_atts']; | |
| 191 | + $gallery_id = $params['id'] ?? ''; | |
| 192 | + $search_slug = $params['search_slug'] ?? ''; | |
| 193 | + $gallery_atts = $params['gallery_atts'] ?? array(); | |
| 194 | + $gallery_atts = is_array( $gallery_atts ) ? $gallery_atts : array(); | |
| 171 | 195 | |
| 172 | 196 | $key = [ |
| 173 | 197 | 'gallery_id' => 'id', |
| 174 | 198 | 'wplr_collection_id' => 'wplr-collection', |
| @@ -174,8 +198,26 @@ | ||
| 174 | 198 | 'wplr_collection_id' => 'wplr-collection', |
| 175 | 199 | 'rml' => 'rml', |
| 176 | 200 | ]; |
| 177 | 201 | |
| 202 | + // This route is public (visitors open galleries from a collection), so everything it | |
| 203 | + // receives is untrusted. The gallery to render is decided by 'search_slug' + 'id' | |
| 204 | + // only: the caller-supplied attributes are stripped of anything that could point the | |
| 205 | + // gallery at other content. Without this, 'collection' could be used to inject | |
| 206 | + // arbitrary shortcodes (reported by JunHee CHO, 2026-09). | |
| 207 | + if ( !isset( $key[ $search_slug ] ) ) { | |
| 208 | + return new WP_REST_Response( [ 'success' => false, 'message' => __( 'Unknown gallery source.', MGL_DOMAIN ) ], 400 ); | |
| 209 | + } | |
| 210 | + $gallery_atts = array_diff_key( $gallery_atts, array_flip( self::$source_atts ) ); | |
| 211 | + | |
| 212 | + // The RML source is a folder path, the others are identifiers. | |
| 213 | + if ( $search_slug !== 'rml' ) { | |
| 214 | + $gallery_id = Meow_MGL_Core::sanitize_id( $gallery_id ); | |
| 215 | + if ( $gallery_id === '' ) { | |
| 216 | + return new WP_REST_Response( [ 'success' => false, 'message' => __( 'Invalid gallery ID.', MGL_DOMAIN ) ], 400 ); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + | |
| 178 | 220 | $shortcode_atts = array(); |
| 179 | 221 | $shortcode_atts[ $key[$search_slug] ] = $gallery_id; |
| 180 | 222 | $shortcode_atts = [...$shortcode_atts, ...$gallery_atts]; |
| 181 | 223 | |
| @@ -209,9 +251,9 @@ | ||
| 209 | 251 | global $wpdb; |
| 210 | 252 | $params = $request->get_json_params( ); |
| 211 | 253 | |
| 212 | 254 | $id = $params['id']; |
| 213 | - $medias = $params['medias']; | |
| 255 | + $medias = Meow_MGL_Core::normalize_medias( $params['medias'] ?? null ); | |
| 214 | 256 | $name = $params['name']; |
| 215 | 257 | $layout = $params['layout']; |
| 216 | 258 | $description = $params['description']; |
| 217 | 259 | $posts = $params['posts']; |
| @@ -227,9 +269,9 @@ | ||
| 227 | 269 | if ( !$name ) { |
| 228 | 270 | throw new Exception( __( 'Please enter a name for your shortcode.', MGL_DOMAIN )); |
| 229 | 271 | } |
| 230 | 272 | |
| 231 | - if ( !$is_post_mode && ( !$medias || !count( $medias['thumbnail_ids'] )) ) { | |
| 273 | + if ( !$is_post_mode && empty( $medias['thumbnail_ids'] ) ) { | |
| 232 | 274 | throw new Exception( __( 'Please select at least one image.', MGL_DOMAIN )); |
| 233 | 275 | } |
| 234 | 276 | |
| 235 | 277 | if ( $is_post_mode && $dynamic_source === 'posts' && ( !$posts && !$latest_posts )) { |
| @@ -411,9 +453,9 @@ | ||
| 411 | 453 | $galleries[$gallery['id']] = [ |
| 412 | 454 | 'name' => $gallery['name'], |
| 413 | 455 | 'description' => $gallery['description'], |
| 414 | 456 | 'layout' => $gallery['layout'], |
| 415 | - 'medias' => unserialize( $gallery['medias'] ), | |
| 457 | + 'medias' => Meow_MGL_Core::hydrate_medias( maybe_unserialize( $gallery['medias'] ) ), | |
| 416 | 458 | 'is_post_mode' => ( bool )$gallery['is_post_mode'], |
| 417 | 459 | 'hero' => ( bool )$gallery['is_hero_mode'], |
| 418 | 460 | 'posts' => $gallery['posts'] ? unserialize( $gallery['posts'] ) : null, |
| 419 | 461 | 'latest_posts' => $gallery['latest_posts'], |
| @@ -523,24 +565,21 @@ | ||
| 523 | 565 | "AND p.ID NOT IN ( " . implode( ', ', array_fill( 0, count( $except ), '%s' )) . " )", $except |
| 524 | 566 | ) : ''; |
| 525 | 567 | $join_clause = ''; |
| 526 | 568 | if ( $unusedImages ) { |
| 527 | - // Retrieve the serialized option from the database | |
| 528 | - $meow_gallery_shortcodes = get_option( 'mgl_shortcodes' ); | |
| 569 | + // Every image used by a gallery, read from the galleries table (this used to read the | |
| 570 | + // old 'mgl_shortcodes' option, which isn't written anymore since the migration). | |
| 571 | + $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes'; | |
| 572 | + Meow_MGL_Migrations::check_db(); | |
| 529 | 573 | |
| 530 | - // Deserialize the option to get the array | |
| 531 | - $shortcodes_array = maybe_unserialize( $meow_gallery_shortcodes ); | |
| 532 | - | |
| 533 | - // Extract all thumbnail IDs from the array | |
| 534 | 574 | $used_thumbnail_ids = []; |
| 535 | - foreach ( $shortcodes_array as $shortcode ) { | |
| 536 | - if ( isset( $shortcode['medias']['thumbnail_ids'] ) && is_array( $shortcode['medias']['thumbnail_ids'] ) ) { | |
| 537 | - $used_thumbnail_ids = array_merge( $used_thumbnail_ids, $shortcode['medias']['thumbnail_ids'] ); | |
| 538 | - } | |
| 575 | + foreach ( $wpdb->get_col( "SELECT medias FROM $shortcodes_table" ) as $medias ) { | |
| 576 | + $medias = Meow_MGL_Core::normalize_medias( maybe_unserialize( $medias ) ); | |
| 577 | + $used_thumbnail_ids = array_merge( $used_thumbnail_ids, $medias['thumbnail_ids'] ); | |
| 539 | 578 | } |
| 540 | 579 | |
| 541 | 580 | // Make sure the IDs are integers |
| 542 | - $used_thumbnail_ids = array_map( 'intval', $used_thumbnail_ids ); | |
| 581 | + $used_thumbnail_ids = array_unique( array_map( 'intval', $used_thumbnail_ids ) ); | |
| 543 | 582 | |
| 544 | 583 | // Include the NOT IN clause to exclude used thumbnail IDs |
| 545 | 584 | if ( !empty( $used_thumbnail_ids ) ) { |
| 546 | 585 | $placeholders = implode( ',', array_fill( 0, count( $used_thumbnail_ids ), '%d' ) ); |
| @@ -623,8 +662,33 @@ | ||
| 623 | 662 | 'data' => $this->core->get_gallery_images( $image_ids, $atts, $layout, $size ) |
| 624 | 663 | ], 200 ); |
| 625 | 664 | } |
| 626 | 665 | |
| 666 | + // Applies WordPress's own visibility rules to a raw posts query: published posts for everyone, | |
| 667 | + // other people's drafts only with edit_others_posts, other people's private posts only with | |
| 668 | + // read_private_posts, and your own in both cases. 'upload_files' (the capability gating this | |
| 669 | + // REST controller) is held by Authors, who must not see the whole site's unpublished content. | |
| 670 | + private function get_post_status_clause( $alias = 'p', $post_type = 'post' ) { | |
| 671 | + global $wpdb; | |
| 672 | + | |
| 673 | + $post_type_object = get_post_type_object( $post_type ); | |
| 674 | + $read_private_cap = $post_type_object ? $post_type_object->cap->read_private_posts : 'read_private_posts'; | |
| 675 | + $edit_others_cap = $post_type_object ? $post_type_object->cap->edit_others_posts : 'edit_others_posts'; | |
| 676 | + | |
| 677 | + $user_id = get_current_user_id(); | |
| 678 | + $clause = "AND ( $alias.post_status = 'publish'"; | |
| 679 | + | |
| 680 | + $clause .= current_user_can( $read_private_cap ) | |
| 681 | + ? " OR $alias.post_status = 'private'" | |
| 682 | + : $wpdb->prepare( " OR ( $alias.post_status = 'private' AND $alias.post_author = %d )", $user_id ); | |
| 683 | + | |
| 684 | + $clause .= current_user_can( $edit_others_cap ) | |
| 685 | + ? " OR $alias.post_status = 'draft'" | |
| 686 | + : $wpdb->prepare( " OR ( $alias.post_status = 'draft' AND $alias.post_author = %d )", $user_id ); | |
| 687 | + | |
| 688 | + return $clause . " ) "; | |
| 689 | + } | |
| 690 | + | |
| 627 | 691 | function rest_fetch_posts( $request ) { |
| 628 | 692 | try { |
| 629 | 693 | $params = $request->get_json_params(); |
| 630 | 694 | $search = isset($params['search']) ? $params['search'] : ''; |
| @@ -639,8 +703,13 @@ | ||
| 639 | 703 | $searchPlaceholder, |
| 640 | 704 | $searchPlaceholder |
| 641 | 705 | ) : ''; |
| 642 | 706 | |
| 707 | + // The same clause is used by both queries on purpose: the search also matches | |
| 708 | + // post_content, so a count taken over a wider set than the rows would let a user probe | |
| 709 | + // the body of posts they cannot read (reported by Kaan Ă–zbek, 2026-09). | |
| 710 | + $where_status_clause = $this->get_post_status_clause( 'p' ); | |
| 711 | + | |
| 643 | 712 | $posts = $wpdb->get_results( |
| 644 | 713 | $wpdb->prepare( |
| 645 | 714 | "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author |
| 646 | 715 | FROM $wpdb->posts p |
| @@ -645,9 +714,9 @@ | ||
| 645 | 714 | "SELECT p.ID, p.post_title, p.post_date, p.post_status, u.display_name as author |
| 646 | 715 | FROM $wpdb->posts p |
| 647 | 716 | LEFT JOIN $wpdb->users u ON p.post_author = u.ID |
| 648 | 717 | WHERE p.post_type = 'post' |
| 649 | - AND p.post_status IN ('publish', 'draft', 'private') | |
| 718 | + $where_status_clause | |
| 650 | 719 | $where_search_clause |
| 651 | 720 | ORDER BY p.post_date DESC |
| 652 | 721 | LIMIT %d, %d", |
| 653 | 722 | $offset, |
| @@ -659,9 +728,9 @@ | ||
| 659 | 728 | $posts_count = (int)$wpdb->get_var( |
| 660 | 729 | "SELECT COUNT(*) |
| 661 | 730 | FROM $wpdb->posts p |
| 662 | 731 | WHERE p.post_type = 'post' |
| 663 | - AND p.post_status IN ('publish', 'draft', 'private') | |
| 732 | + $where_status_clause | |
| 664 | 733 | $where_search_clause" |
| 665 | 734 | ); |
| 666 | 735 | |
| 667 | 736 | $data = array_map(function($post) { |