Skip to content

Commit

Permalink
See #463
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Jul 18, 2017
1 parent c6c7278 commit fc3485c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/class-kirki-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private function default_modules() {
'tooltips' => 'Kirki_Modules_Tooltips',
'branding' => 'Kirki_Modules_Customizer_Branding',
'postMessage' => 'Kirki_Modules_PostMessage',
// 'post_meta' => 'Kirki_Modules_Post_Meta',
'selective-refresh' => 'Kirki_Modules_Selective_Refresh',
'field-dependencies' => 'Kirki_Modules_Field_Dependencies',
'custom-sections' => 'Kirki_Modules_Custom_Sections',
Expand Down
91 changes: 91 additions & 0 deletions modules/post-meta/class-kirki-modules-post-meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Customize_Queried_Post_Info class.
*
* @package CustomizeQueriedPostInfo
*/

/**
* Class Customize_Queried_Post_Info.
*/
class Kirki_Modules_Post_Meta {

/**
* The object instance.
*
* @static
* @access private
* @since 3.0.0
* @var object
*/
private static $instance;

/**
* Gets an instance of this object.
* Prevents duplicate instances which avoid artefacts and improves performance.
*
* @static
* @access public
* @since 3.0.0
* @return object
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Constructor.
*
* @access protected
* @since 3.1.0
*/
protected function __construct() {

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.
*
* @access public
* @since 3.1.0
*/
public function enqueue_control_scripts() {

wp_enqueue_script( 'kirki_post_meta_previewed_controls', trailingslashit( Kirki::$url ) . 'modules/post-meta/customize-controls.js', array( 'jquery', 'customize-controls' ), false, true );
}

/**
* Initialize Customizer preview.
*
* @access public
* @since 3.1.0
*/
public function customize_preview_init() {

add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
}

/**
* Enqueue script for Customizer preview.
*
* @access public
* @since 3.1.0
*/
public function enqueue_preview_scripts() {

wp_enqueue_script( 'kirki_post_meta_previewed_preview', trailingslashit( Kirki::$url ) . 'modules/post-meta/customize-preview.js', array( 'jquery', 'customize-preview' ), false, true );

$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( 'kirki_post_meta_previewed_preview', 'data', sprintf( 'var _customizePostPreviewedQueriedObject = %s;', wp_json_encode( $queried_post ) ) );
}
}
24 changes: 24 additions & 0 deletions modules/post-meta/customize-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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 );
} );
} );

// Listen for post
self.queriedPost.bind( function( newPost, oldPost ) {
window.kirkiPost = false;
if ( newPost || oldPost ) {
window.kirkiPost = ( newPost ) ? newPost : oldPost;
}
} );
} )( wp.customize );
19 changes: 19 additions & 0 deletions modules/post-meta/customize-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* global wp, _customizePostPreviewedQueriedObject */
( function() {

var self;

self = {
queriedPost: null
};
if ( ! _.isUndefined( _customizePostPreviewedQueriedObject ) ) {
self.queriedPost = _customizePostPreviewedQueriedObject;
}

// Send the queried post object to the Customizer pane when ready.
wp.customize.bind( 'preview-ready', function() {
wp.customize.preview.bind( 'active', function() {
wp.customize.preview.send( 'queried-post', self.queriedPost );
} );
} );
} )( wp.customize );

0 comments on commit fc3485c

Please sign in to comment.