re_list implements ArrayAccess any array-specific actions must be taken on // $picture_list->container or they won't do anything. $picture_list = new LegacyImageCollection(); $current_pid = null; // begin processing. $current_page = ( @\get_the_ID() == false ) ? 0 : @\get_the_ID(); // determine what the "current image" is; used mostly for carousel. if ( ! is_numeric( $pid ) && ! empty( $pid ) ) { $picture = $image_map->find_first( [ 'image_slug = %s', $pid ] ); $pid = $picture->$image_key; } // create our new wrappers. foreach ( $images as &$image ) { if ( $image && isset( $params['effect_code'] ) ) { if ( is_object( $image ) ) { $image->thumbcode = $params['effect_code']; } elseif ( is_array( $image ) ) { $image['thumbcode'] = $params['effect_code']; } } $new_image = new LegacyImage( $image, $displayed_gallery ); if ( $pid == $new_image->$image_key ) { $current_pid = $new_image; } $picture_list[] = $new_image; } reset( $picture_list->container ); // assign current_pid. $current_pid = ( is_null( $current_pid ) ) ? current( $picture_list->container ) : $current_pid; foreach ( $picture_list as &$image ) { if ( isset( $image->hidden ) && $image->hidden ) { $tmp = $displayed_gallery->display_settings['number_of_columns']; $image->style = ( $tmp > 0 ) ? 'style="width:' . floor( 100 / $tmp ) . '%;display: none;"' : 'style="display: none;"'; } } // find our gallery to build the new one on. $orig_gallery = $gallery_map->find( current( $picture_list->container )->galleryid ); // create the 'gallery' object. $gallery = new \stdclass(); $gallery->ID = $displayed_gallery->id(); $gallery->name = stripslashes( $orig_gallery->name ); $gallery->title = stripslashes( $orig_gallery->title ); $gallery->description = \html_entity_decode( \stripslashes( $orig_gallery->galdesc ) ); $gallery->pageid = $orig_gallery->pageid; $gallery->anchor = 'ngg-gallery-' . $gallery_id . '-' . $current_page; $gallery->displayed_gallery = &$displayed_gallery; $gallery->columns = @intval( $displayed_gallery->display_settings['number_of_columns'] ); $gallery->imagewidth = ( $gallery->columns > 0 ) ? 'style="width:' . floor( 100 / $gallery->columns ) . '%;"' : ''; if ( ! empty( $displayed_gallery->display_settings['show_slideshow_link'] ) ) { $gallery->show_slideshow = true; $gallery->slideshow_link = $params['slideshow_link']; $gallery->slideshow_link_text = $displayed_gallery->display_settings['slideshow_link_text']; } else { $gallery->show_slideshow = false; } $gallery = apply_filters( 'ngg_gallery_object', $gallery, 4 ); // build our array of things to return. $return = [ 'gallery' => $gallery ]; // single_image is an internally added flag. if ( ! empty( $params['single_image'] ) ) { $return['image'] = $picture_list[0]; } else { $return['current'] = $current_pid; $return['images'] = $picture_list->container; } // this is expected to always exist. if ( ! empty( $params['pagination'] ) ) { $return['pagination'] = $params['pagination']; } else { $return['pagination'] = null; } if ( ! empty( $params['next'] ) ) { $return['next'] = $params['next']; } else { $return['next'] = false; } if ( ! empty( $params['prev'] ) ) { $return['prev'] = $params['prev']; } else { $return['prev'] = false; } return $return; } /** * Returns an url to view the displayed gallery using an alternate display type * * @param DisplayedGallery $displayed_gallery * @param string $display_type * @return string */ public function get_url_for_alternate_display_type( $displayed_gallery, $display_type, $origin_url = false ) { $app = Router::get_instance()->get_routed_app(); if ( ! $origin_url && ! empty( $displayed_gallery->display_settings['original_display_type'] ) && ! empty( $_SERVER['NGG_ORIG_REQUEST_URI'] ) ) { $origin_url = $_SERVER['NGG_ORIG_REQUEST_URI']; } $url = ( $origin_url ?: $app->get_app_url( false, true ) ); $url = $app->remove_parameter( 'show', $displayed_gallery->id(), $url ); $url = $app->set_parameter( 'show', $display_type, $displayed_gallery->id(), false, $url ); return $url; } /** * Returns a formatted HTML string of a pagination widget * * @param mixed $selected_page * @param int $number_of_entities * @param int $entities_per_page * @param string|null $current_url (optional) * @return array Of data holding prev & next url locations and a formatted HTML string */ public function create_pagination( $selected_page, $number_of_entities, $entities_per_page = 0, $current_url = null ) { $router = Router::get_instance(); $app = $router->get_routed_app(); $prev_symbol = \apply_filters( 'ngg_prev_symbol', '◄' ); $next_symbol = \apply_filters( 'ngg_next_symbol', '►' ); if ( empty( $current_url ) ) { $current_url = $app->get_app_url( false, true ); if ( \is_archive() ) { $id = \get_the_ID(); if ( $id == null ) { global $post; $id = $post ? $post->ID : null; } if ( $id != null && \in_the_loop() ) { $current_url = \get_permalink( $id ); } } } // Early exit. $return = [ 'prev' => '', 'next' => '', 'output' => "
", ]; if ( $entities_per_page <= 0 || $number_of_entities <= 0 ) { return $return; } // Construct array of page urls. $ending_ellipsis = $starting_ellipsis = false; $number_of_pages = ceil( $number_of_entities / $entities_per_page ); $pages = []; for ( $i = 1; $i <= $number_of_pages; $i++ ) { if ( $selected_page === $i ) { $pages['current'] = "{$i}"; } else { $link = esc_attr( $app->set_parameter( 'nggpage', $i, null, false, $current_url ) ); $pages[ $i ] = "{$i}"; } } $after = $this->array_slice_from( 'current', $pages ); if ( count( $after ) > 3 ) { $after = array_merge( $this->array_take_from_start( 2, $after ), [ "..." ], $this->array_take_from_end( 1, $after ) ); } $before = $this->array_slice_to( 'current', $pages ); if ( count( $before ) > 3 ) { $before = array_merge( $this->array_take_from_start( 1, $before ), [ "..." ], $this->array_take_from_end( 2, $before ) ); array_pop( $before ); } $pages = array_merge( $before, $after ); if ( $pages && count( $pages ) > 1 ) { // Next page. if ( $selected_page + 1 <= $number_of_pages ) { $next_page = $selected_page + 1; $link = $return['next'] = $app->set_parameter( 'nggpage', $next_page, null, false, $current_url ); $pages[] = ""; } // Prev page. if ( $selected_page - 1 > 0 ) { $prev_page = $selected_page - 1; $link = $return['next'] = $app->set_parameter( 'nggpage', $prev_page, null, false, $current_url ); array_unshift( $pages, "" ); } $return['output'] = "
" . implode( "\n", $pages ) . '
'; } return $return; } /** * This is necessary for the SinglePicture display type. * * @return false */ public function is_hidden_from_igw() { return false; } public function array_slice_from( $find_key, $arr ) { $retval = []; reset( $arr ); foreach ( $arr as $key => $value ) { if ( $key == $find_key || $retval ) { $retval[ $key ] = $value; } } reset( $arr ); return $retval; } public function array_slice_to( $find_key, $arr ) { $retval = []; reset( $arr ); foreach ( $arr as $key => $value ) { $retval[ $key ] = $value; if ( $key == $find_key ) { break; } } reset( $arr ); return $retval; } public function array_take_from_start( $number, $arr ) { $retval = []; foreach ( $arr as $key => $value ) { if ( count( $retval ) < $number ) { $retval[ $key ] = $value; } else { break; } } return $retval; } public function array_take_from_end( $number, $arr ) { return array_reverse( $this->array_take_from_start( $number, array_reverse( $arr ) ) ); } /* The following methods manage the installation and removal of display types */ /** * @param $name */ public function delete_duplicates( $name ) { $mapper = DisplayTypeMapper::get_instance(); $results = $mapper->find_all( [ 'name = %s', $name ] ); if ( count( $results ) > 0 ) { array_pop( $results ); // the last should be the latest. foreach ( $results as $display_type ) { $mapper->destroy( $display_type ); } } $mapper->flush_query_cache(); } /** * Method for installing a display type. * * @param string $name Display type name. * @param array $properties Display type properties. * @param bool $reset True: revert to default setting. * @return bool|int */ public function install_display_type( string $name, array $properties = [], bool $reset = false ) { $this->delete_duplicates( $name ); // Try to find the existing entity. If it doesn't exist, we'll create. $mapper = DisplayTypeMapper::get_instance(); $display_type = $mapper->find_by_name( $name ); $mapper->flush_query_cache(); if ( ! $display_type ) { $display_type = new DisplayType(); } // Update the properties of the display type. $properties['name'] = $name; $changed = false; foreach ( $properties as $key => $val ) { if ( ! isset( $display_type->$key ) || empty( $display_type->$key ) || is_null( $display_type->$key ) || $reset ) { $display_type->$key = $val; $changed = true; } } // Save the entity. if ( $changed ) { return $mapper->save( $display_type ); } return false; } /** * Uninstalls all display types */ public function uninstall_display_types() { $mapper = DisplayTypeMapper::get_instance(); $mapper->delete()->run_query(); } /** * @param bool $reset (optional) Unused */ public function install( $reset = false ) { } /** * @param bool $hard (optional) Unused */ public function uninstall( $hard = false ) { Transient::flush(); $this->uninstall_display_types(); } public function get_default_settings() { return []; } }