Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout Directory: Add Category & Niches Support #1104

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
57 changes: 57 additions & 0 deletions css/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -2117,10 +2117,63 @@
flex-flow: row wrap;
}

.so-directory-items-filters {
display: flex;
gap: 24px;

& > div {
display: flex;
}

ul {
margin: 0;
}

span,
li {
display: inline-block;
padding: 5px 7.5px;
}

span {
font-weight: 600;
}

li {
cursor: pointer;
text-decoration: underline;
text-transform: capitalize;

&:focus,
&:hover {
background: #f3f3f3;
text-decoration: none;
}

&.so-active-filter {
background: #f3f3f3;
cursor: default;
text-decoration: none;
}
}
}

// For a list of directory items
.so-directory-items {

&.so-empty {

.so-no-results {
display: block;
}

.so-directory-pages {
display: none;
}
}

.so-no-results {
display: none;
margin: 20px 0;
padding: 0 5px;
}
Expand All @@ -2130,6 +2183,10 @@
.box-sizing(border-box);
padding: 6px;

&.so-hidden {
display: none;
}

.so-directory-item-wrapper {
display: flex;
flex-flow: column nowrap;
Expand Down
69 changes: 46 additions & 23 deletions inc/admin-layouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,28 @@ public function action_get_prebuilt_layouts() {

$return['max_num_pages'] = 1;
} elseif ( substr( $type, 0, 10 ) == 'directory-' ) {
$return['title'] = __( 'Layouts Directory', 'siteorigin-panels' );

// This is a query of the prebuilt layout directory
$query = array();

if ( ! empty( $search ) ) {
$query['search'] = $search;
}
$query['page'] = $page_num;

$directory_id = str_replace( 'directory-', '', $type );
$directories = $this->get_directories();
$directory = ! empty( $directories[ $directory_id ] ) ? $directories[ $directory_id ] : false;

if ( empty( $directory ) ) {
return false;
}
// Check if we previously cached this directory result.

$cache = get_transient( 'siteorigin_panels_layouts_directory_' . $directory_id .'_page_' . $page_num );

if ( empty( $search ) && ! empty( $cache ) ) {
$return = $cache;
} else {
$return['title'] = __( 'Layouts Directory', 'siteorigin-panels' );

// This is a query of the prebuilt layout directory
$query = array();
$directories = $this->get_directories();
$directory = ! empty( $directories[ $directory_id ] ) ? $directories[ $directory_id ] : false;

if ( empty( $directory ) ) {
return false;
}

$url = add_query_arg( $query, $directory[ 'url' ] . 'wp-admin/admin-ajax.php?action=query_layouts' );

if ( ! empty( $directory['args'] ) && is_array( $directory['args'] ) ) {
$url = add_query_arg( $directory['args'], $url );
if ( ! empty( $directory[ 'args' ] ) && is_array( $directory[ 'args' ] ) ) {
$url = add_query_arg( $directory[ 'args' ], $url );
}

$url = apply_filters( 'siteorigin_panels_layouts_directory_url', $url );
Expand All @@ -280,19 +275,31 @@ public function action_get_prebuilt_layouts() {
$item['id'] = esc_html( $item['slug'] );
$item['type'] = esc_html( $type );

if ( ! empty( $item['access'] ) ) {
$item['access'] = esc_html( $item['access'] );
$item['category'] = esc_html( $item['category'] );
$niches = ( empty( $item['niches'] ) ? '' : ' ' . implode( ' ', json_decode( $item['niches'] ) ) );
$item['niches'] = esc_html( $niches );
$item['class'] = esc_html( $item['category'] ) . ( empty( $item['niches'] ) ? '' : ' ' . esc_html( $niches ) );
}

if ( empty( $item['screenshot'] ) && ! empty( $item['preview'] ) ) {
$preview_url = add_query_arg( 'screenshot', 'true', $item[ 'preview' ] );
$item['screenshot'] = esc_url( 'https://s.wordpress.com/mshots/v1/' . urlencode( $preview_url ) . '?w=700' );
}

$return['items'][] = $item;
}
}

$return['max_num_pages'] = $results['max_num_pages'];
set_transient( 'siteorigin_panels_layouts_directory_' . $directory_id .'_page_' . $page_num, $return, 86400 );
if ( ! empty( $results['niches'] ) ) {
$return['niches'] = $this->escape_results( $results['niches'] );
$return['categories'] = $this->escape_results( $results['categories'] );
}
}
set_transient( 'siteorigin_panels_layouts_directory_' . $directory_id, $results, 3600 );
}
}
$no_search_title = true;
} elseif ( strpos( $type, 'clone_' ) !== false ) {
// Check that the user can view the given page types
$post_type = get_post_type_object( str_replace( 'clone_', '', $type ) );
Expand Down Expand Up @@ -337,7 +344,7 @@ public function action_get_prebuilt_layouts() {
}

// Add the search part to the title
if ( ! empty( $search ) ) {
if ( ! empty( $search ) && empty( $no_search_title ) ) {
$return['title'] .= __( ' - Results For:', 'siteorigin-panels' ) . ' <em>' . esc_html( $search ) . '</em>';
}

Expand All @@ -346,6 +353,22 @@ public function action_get_prebuilt_layouts() {
wp_die();
}

/**
* Escapes the keys and values of an array using the `esc_html` function.
*
* @param array $results The array to escape.
* @return array The escaped array.
*/
private function escape_results( $results = array() ) {
$escaped_values = array();
foreach ( $results as $key => $value ) {
$escaped_key = esc_html( $key );
$escaped_value = esc_html( $value );
$escaped_values[ $escaped_key ] = $escaped_value;
}
return $escaped_values;
}

private function delete_file( $file ) {
if ( ! empty( $file ) && file_exists( $file ) ) {
@unlink( $file );
Expand Down
Loading