diff --git a/js/customize-nav-menus-posts-extensions.js b/js/customize-nav-menus-posts-extensions.js new file mode 100644 index 0000000..b9fb1e5 --- /dev/null +++ b/js/customize-nav-menus-posts-extensions.js @@ -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 ); diff --git a/js/customize-posts.js b/js/customize-posts.js index f2775da..31dff0a 100644 --- a/js/customize-posts.js +++ b/js/customize-posts.js @@ -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 } ) ); }; /** diff --git a/php/class-customize-posts-plugin.php b/php/class-customize-posts-plugin.php index 71ce606..22c8649 100644 --- a/php/class-customize-posts-plugin.php +++ b/php/class-customize-posts-plugin.php @@ -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__ ) ); diff --git a/php/class-wp-customize-posts.php b/php/class-wp-customize-posts.php index 26460e7..4c28951 100644 --- a/php/class-wp-customize-posts.php +++ b/php/class-wp-customize-posts.php @@ -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();