Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Insert full posts instead of stubs via nav menus #287

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions js/customize-nav-menus-posts-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(function( api, $ ) {
'use strict';

if ( api.Menus.insertAutoDraftPost ) {

/**
* Insert a new `auto-draft` post.
*
* @param {object} params - Parameters for the draft post to create.
* @param {string} params.post_type - Post type to add.
* @param {string} params.post_title - Post title to use.
* @return {jQuery.promise} Promise resolved with the added post.
*/
api.Menus.insertAutoDraftPost = function insertAutoDraftPost( params ) {

var insertPromise, deferred = $.Deferred();

insertPromise = api.Posts.insertAutoDraftPost( params.post_type );

insertPromise.done( function insertAutoDraftPostDone( data ) {
var postData;

postData = _.clone( data.setting.get() );
postData.post_title = params.post_title;
postData.post_status = 'publish';
data.setting.set( postData );

deferred.resolve( {
post_id: data.postId,
url: api.Posts.getPostUrl( { post_id: data.postId, post_type: params.post_type } )
} );
} );
insertPromise.fail( function insertAutoDraftPostFail( failure ) {
deferred.reject( failure );
} );

return deferred.promise();
};
}

})( wp.customize, jQuery );
48 changes: 32 additions & 16 deletions js/customize-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,47 @@
};

/**
* Get the post preview URL.
* Get post URL.
*
* @param {object} params - Parameters to configure the preview URL.
* @param {number} params.post_id - Post ID to preview.
* @param {string} [params.post_type] - Post type to preview.
* @param {object} params - Query vars.
* @param {number} params.post_id - Post ID.
* @param {string} [params.post_type] - Post type.
* @param {boolean} [params.preview] - .
* @return {string} Preview URL.
*/
component.getPreviewUrl = function( params ) {
var url = api.settings.url.home, args = {};
component.getPostUrl = function getPostUrl( params ) {
var queryVars, postId;

queryVars = _.clone( params );
postId = parseInt( queryVars.post_id, 10 );

if ( ! params || ! params.post_id ) {
throw new Error( 'Missing params' );
if ( ! postId ) {
throw new Error( 'Missing post_id param.' );
}
delete queryVars.post_id;

args.preview = true;
if ( 'page' === params.post_type ) {
args.page_id = params.post_id;
if ( queryVars.post_type && 'page' === queryVars.post_type ) {
queryVars.page_id = postId;
} else {
args.p = params.post_id;
if ( params.post_type && 'post' !== params.post_type ) {
args.post_type = params.post_type;
}
queryVars.p = postId;
}
if ( 'post' === params.post_type || 'page' === params.post_type ) {
delete queryVars.post_type;
}

return url + '?' + $.param( args );
return api.settings.url.home + '?' + $.param( queryVars );
};

/**
* Get the post preview URL.
*
* @param {object} params - Parameters to configure the preview URL.
* @param {number} params.post_id - Post ID to preview.
* @param {string} [params.post_type] - Post type to preview.
* @return {string} Preview URL.
*/
component.getPreviewUrl = function getPreviewUrl( params ) {
return component.getPostUrl( _.extend( {}, params, { preview: true } ) );
};

/**
Expand Down
6 changes: 6 additions & 0 deletions php/class-customize-posts-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ public function register_scripts( WP_Scripts $wp_scripts ) {
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );

$handle = 'customize-nav-menus-posts-extensions';
$src = plugins_url( 'js/customize-nav-menus-posts-extensions' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-posts', 'customize-nav-menus' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );

// This can be incorporated into customize-preview.js during 4.7.
$handle = 'customize-preview-setting-validities';
$src = plugins_url( 'js/customize-preview-setting-validities' . $suffix, dirname( __FILE__ ) );
Expand Down
5 changes: 5 additions & 0 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ public function filter_customize_save_response_to_export_saved_values( $response
public function enqueue_scripts() {
wp_enqueue_script( 'customize-posts' );
wp_enqueue_style( 'customize-posts' );

if ( isset( $this->manager->nav_menus ) ) {
wp_enqueue_script( 'customize-nav-menus-posts-extensions' );
}

$this->enqueue_select2_locale_script();

$post_types = array();
Expand Down