-
Notifications
You must be signed in to change notification settings - Fork 34
Feature / Add edit in customizer buttons #140
base: develop
Are you sure you want to change the base?
Changes from 14 commits
d0c02da
d35ffc6
984b7cd
dca990b
3bb9949
d18fb24
8011d97
b54051e
1ae5186
ddeb2ff
b0e2f20
2d54429
d39dfd1
a335d32
e535780
9cb8cb6
f7e53ff
986c7f4
d41a4f6
e0589bc
1ba5017
bfdb6c7
04d19b9
3ab205a
f981ad5
3e5b3f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,10 @@ public function __construct( Customize_Posts_Plugin $plugin ) { | |
| add_action( 'customize_controls_init', array( $this, 'remove_static_controls_and_sections' ), 100 ); | ||
| add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_customize_scripts' ) ); | ||
| add_action( 'customize_preview_init', array( $this, 'make_auto_draft_status_previewable' ) ); | ||
| add_action( 'admin_footer', array( $this, 'add_edit_customizer_button_posts' ) ); | ||
| add_filter( 'post_row_actions', array( $this, 'add_edit_customizer_to_row_actions' ), 10, 2 ); | ||
| add_filter( 'page_row_actions', array( $this, 'add_edit_customizer_to_row_actions' ), 10, 2 ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -101,25 +105,84 @@ public function enqueue_admin_scripts() { | |
| return; | ||
| } | ||
| wp_enqueue_script( 'edit-post-preview-admin' ); | ||
| $post = $this->get_previewed_post(); | ||
| $customize_url = self::get_customize_url(); | ||
|
|
||
| $data = array( | ||
| 'customize_url' => $customize_url, | ||
| ); | ||
|
|
||
| wp_scripts()->add_data( 'edit-post-preview-admin', 'data', sprintf( 'var _editPostPreviewAdminExports = %s;', wp_json_encode( $data ) ) ); | ||
| wp_enqueue_script( 'customize-loader' ); | ||
| wp_add_inline_script( 'edit-post-preview-admin', 'jQuery( function() { EditPostPreviewAdmin.init(); } );', 'after' ); | ||
| } | ||
|
|
||
| /** | ||
| * Add the edit customizer to row actions for Posts/Pages. | ||
| * | ||
| * @param array $actions Actions. | ||
| * @param object $post Post. | ||
| * @return array $rebuild_actions | ||
| */ | ||
| public function add_edit_customizer_to_row_actions( $actions, $post ) { | ||
| if ( ! ( $post instanceof WP_Post ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| // We need to make sure the current post type has show_in_customizer to true or if it's default post type is post or page. | ||
| $post_type_object = get_post_types( array(), 'objects' ); | ||
| if ( ! isset( $post_type_object[ get_post_type() ]->show_in_customizer ) && 'post' !== get_post_type() && 'page' !== get_post_type() || ! current_user_can( 'edit_post', $post->ID ) ) { | ||
| return $actions; | ||
| } | ||
|
|
||
| // Let's rebuild the start of our array. | ||
| $rebuild_actions = array(); | ||
| $rebuild_actions['edit'] = $actions['edit']; | ||
| $rebuild_actions['edit_customizer'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( self::get_customize_url( $post ) ), esc_html__( 'Edit in Customizer', 'customize-posts' ) ); | ||
|
|
||
| return array_merge( $rebuild_actions, $actions ); | ||
| } | ||
|
|
||
| /** | ||
| * Add the Edit in Customizer button to the edit post screen. | ||
| */ | ||
| public function add_edit_customizer_button_posts() { | ||
| $post_type_object = get_post_types( array(), 'objects' ); | ||
| if ( ! isset( $post_type_object[ get_post_type() ]->show_in_customizer ) && 'post' !== get_post_type() && 'page' !== get_post_type() || '' !== get_current_screen()->action ) { | ||
| return false; | ||
| } | ||
|
|
||
| printf( '<a id="%1$s" class="%2$s" href="%3$s">%4$s</a>', esc_html__( 'customize-button', 'customize-posts' ), esc_html__( 'page-title-action hide-if-no-customize', 'customize-posts' ), esc_url( self::get_customize_url() ), esc_html__( 'Edit in Customizer', 'customize-posts' ) ); | ||
| wp_add_inline_script( 'edit-post-preview-admin', 'jQuery( \'#customize-button\' ).appendTo( \'.wrap h1\' )', 'after' ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the customize line. | ||
| * | ||
| * @param array $post Post. | ||
| * @return string $customize_url | ||
| */ | ||
| public function get_customize_url( $post = null ) { | ||
| if ( ! is_a( $post, 'WP_Post' ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one could also be updated. |
||
| $post = $this->get_previewed_post(); | ||
| } | ||
|
|
||
| if ( ! $post ) { | ||
| return false; | ||
| } | ||
|
|
||
| $id_param = ( 'page' === $post->post_type ) ? 'page_id' : 'p'; | ||
| $url = get_preview_post_link( $post, array(), home_url( '?preview=true&' . $id_param . '=' . $post->ID ) ); | ||
|
|
||
| $url = get_preview_post_link( $post, array(), get_permalink( $post->ID ) . '?preview=true&' . $id_param . '=' . $post->ID ); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @westonruter I've added get_permalink here as I can't work out how we use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can get the URL with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @valendesigns Thanks! I'll do this tonight :) |
||
| $customize_url = add_query_arg( | ||
| array( | ||
| 'url' => urlencode( $url ), | ||
| 'previewed_post' => $post->ID, | ||
| 'autofocus[section]' => sprintf( 'post[%s][%d]', $post->post_type, $post->ID ), | ||
| self::PREVIEW_POST_NONCE_QUERY_VAR => wp_create_nonce( self::PREVIEW_POST_NONCE_ACTION ), | ||
| ), | ||
| wp_customize_url() | ||
| ); | ||
| $data = array( | ||
| 'customize_url' => $customize_url, | ||
| ); | ||
| wp_scripts()->add_data( 'edit-post-preview-admin', 'data', sprintf( 'var _editPostPreviewAdminExports = %s;', wp_json_encode( $data ) ) ); | ||
| wp_enqueue_script( 'customize-loader' ); | ||
| wp_add_inline_script( 'edit-post-preview-admin', 'jQuery( function() { EditPostPreviewAdmin.init(); } );', 'after' ); | ||
|
|
||
| return $customize_url; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably loop over all post types that are are eligible for showing in the Customizer instead of hard-coding just two. There is a method for getting all post types and code in the plugin for determining if the post type should
show_in_customizer