Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 2, 2020 07:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/1feb78845c8e562dd59d to your computer and use it in GitHub Desktop.
Save westonruter/1feb78845c8e562dd59d to your computer and use it in GitHub Desktop.
Customize Queried Post Info plugin: Communicate the singular post queried object currently previewed in the Customizer.
<?php
/**
* Customize_Queried_Post_Info class.
*
* @package CustomizeQueriedPostInfo
*/
/**
* Class Customize_Queried_Post_Info.
*/
class Customize_Queried_Post_Info {
/**
* Add hooks for plugin.
*/
public function init() {
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
}
/**
* Enqueue Customizer control scripts.
*/
public function enqueue_control_scripts() {
$handle = 'customize-post-previewed-controls';
$src = plugins_url( 'customize-controls.js', __FILE__ );
$deps = array( 'customize-controls' );
$ver = false;
$in_footer = true;
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
}
/**
* Initialize Customizer preview.
*/
public function customize_preview_init() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
}
/**
* Enqueue script for Customizer preview.
*/
public function enqueue_preview_scripts() {
$handle = 'customize-post-previewed-preview';
$src = plugins_url( 'customize-preview.js', __FILE__ );
$deps = array( 'customize-preview' );
$ver = false;
$in_footer = true;
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
$wp_scripts = wp_scripts();
$queried_post = null;
if ( is_singular() && get_queried_object() ) {
$queried_post = get_queried_object();
$queried_post->meta = get_post_custom( $queried_post->id );
}
$wp_scripts->add_data( $handle, 'data', sprintf( 'var _customizePostPreviewedQueriedObject = %s;', wp_json_encode( $queried_post ) ) );
}
}
/* global wp, console */
( function( api ) {
var self;
self = {
queriedPost: new api.Value()
};
/**
* Listen for queried-post messages from the preview.
*/
api.bind( 'ready', function() {
api.previewer.bind( 'queried-post', function( queriedPost ) {
self.queriedPost.set( queriedPost || false ); /* Note that Value.set ignores null, hence false here. */
} );
} );
/**
* Example plugin code that can now listen for when a post is being previewed or ceases to be previewed.
*/
self.queriedPost.bind( function( newPost, oldPost ) {
if ( newPost ) {
if ( oldPost ) {
console.info( 'You are no longer previewing: ' + oldPost.post_title + ' (post ID: ' + oldPost.ID + ')' );
}
console.info( 'You are now previewing: ' + newPost.post_title + ' (post ID: ' + newPost.ID + ')' );
} else {
console.info( 'You are no longer previewing a singular post template.' );
}
} );
} )( wp.customize );
/* global wp, _customizePostPreviewedQueriedObject */
( function( api ) {
var self;
self = {
queriedPost: null
};
if ( 'undefined' !== typeof _customizePostPreviewedQueriedObject ) {
self.queriedPost = _customizePostPreviewedQueriedObject;
}
/**
* Send the queried post object to the Customizer pane when ready.
*/
api.bind( 'preview-ready', function() {
api.preview.bind( 'active', function() {
api.preview.send( 'queried-post', self.queriedPost );
} );
} );
} )( wp.customize );
<?php
/**
* Plugin Name: Customize Queried Post Info
* Plugin URI: https://gist.github.com/westonruter/1feb78845c8e562dd59d
* Description: Communicate the singular post queried object currently previewed in the Customizer.
* Version: 0.1
* Author: XWP, Weston Ruter
* Author URI: https://xwp.co/
* License: GPLv2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* Copyright (c) 2015 XWP (https://xwp.co/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package CustomizeQueriedPostInfo
*/
require_once __DIR__ . '/class-customize-queried-post-info.php';
$customize_queried_post_info = new Customize_Queried_Post_Info();
$customize_queried_post_info->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment