__( 'Not allowed.', 'advanced-css3-related-posts-widget' ) ), 403 ); } } /** * Render a preview from unsaved form values. */ public static function preview() { check_ajax_referer( 'srpp_admin', 'nonce' ); self::guard(); $raw = isset( $_POST['srpp'] ) ? wp_unslash( $_POST['srpp'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput if ( ! is_array( $raw ) ) { wp_send_json_error( array( 'message' => __( 'Nothing to preview.', 'advanced-css3-related-posts-widget' ) ) ); } $config = SRPP_Display::merge( SRPP_Display::sanitize_config( SRPP_Editor::normalize_input( $raw ) ) ); $display = new SRPP_Display(); $display->id = isset( $_POST['display_id'] ) ? absint( wp_unslash( $_POST['display_id'] ) ) : 0; $display->status = 'publish'; $display->config = $config; $display->title = isset( $_POST['srpp_title'] ) ? sanitize_text_field( wp_unslash( $_POST['srpp_title'] ) ) : ''; SRPP_Engine::reset_used(); $source = self::sample_source( $config ); $html = srpp()->render->render( $display, array( 'post_id' => $source, 'placement' => 'preview', 'preview' => true, ) ); if ( '' === $html ) { $ids = get_posts( array( 'post_type' => (array) $config['target_types'], 'post_status' => 'publish', 'posts_per_page' => (int) $config['items'], 'fields' => 'ids', 'no_found_rows' => true, ) ); if ( $ids ) { $html = srpp()->render->render( $display, array( 'ids' => $ids, 'placement' => 'preview', 'preview' => true, ) ); } } if ( '' === $html ) { wp_send_json_success( array( 'html' => '', 'empty' => __( 'No content matched these settings yet — publish some items in the selected content type.', 'advanced-css3-related-posts-widget' ), ) ); } wp_send_json_success( array( 'html' => $html, 'source' => $source ? get_the_title( $source ) : '', ) ); } /** * Pick a representative post to preview against. * * @param array $config Config. * @return int */ protected static function sample_source( $config ) { $types = ! empty( $config['cond_types'] ) ? (array) $config['cond_types'] : (array) $config['target_types']; $ids = get_posts( array( 'post_type' => $types, 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids', 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, ) ); return $ids ? (int) $ids[0] : 0; } /** * Content search for the pickers. */ public static function search() { check_ajax_referer( 'srpp_admin', 'nonce' ); self::guard(); $term = isset( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : ''; $types = isset( $_POST['types'] ) ? sanitize_text_field( wp_unslash( $_POST['types'] ) ) : ''; if ( strlen( $term ) < 2 ) { wp_send_json_success( array() ); } $post_types = $types ? array_filter( array_map( 'sanitize_key', explode( ',', $types ) ) ) : array_keys( srpp_post_types() ); $posts = get_posts( array( 'post_type' => $post_types, 'post_status' => array( 'publish', 'draft', 'private' ), 's' => $term, 'posts_per_page' => 15, 'no_found_rows' => true, ) ); $out = array(); $labels = srpp_post_types(); foreach ( $posts as $post ) { $out[] = array( 'id' => (int) $post->ID, 'title' => html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ), 'type' => isset( $labels[ $post->post_type ] ) ? $labels[ $post->post_type ] : $post->post_type, ); } wp_send_json_success( $out ); } }