-
Notifications
You must be signed in to change notification settings - Fork 36
Feature / Add edit in customizer buttons #140
base: develop
Are you sure you want to change the base?
Changes from 4 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,8 @@ 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_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,7 +103,54 @@ 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' ); | ||
|
|
||
| if ( 'add' !== get_current_screen()->action ) { | ||
| wp_add_inline_script( 'edit-post-preview-admin', 'jQuery( \'.wrap h1\' ).append( \'<a class="page-title-action hide-if-no-customize" href="' . esc_url( $customize_url ) . '">Edit in Customizer</a>\' )', 'after' ); | ||
|
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. I'd suggest adding the link in the footer, and then use jQuery to move this existing link in the document into the right place, instead putting the markup into a JS string. Also needs translation function. |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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 ( ! 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. Let's be consistent and use the same coding style. Other places in the plugin we would check if
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've now fixed it. |
||
| return false; | ||
| } | ||
|
|
||
| // Let's rebuild the order of $actions. | ||
| $rebuild_actions = array(); | ||
| $rebuild_actions['edit'] = $actions['edit']; | ||
| $rebuild_actions['edit_customizer'] = '<a href="' . esc_html( self::get_customize_url( $post ) ) . '">Edit in Customizer</a>'; | ||
|
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.
Also, use |
||
| $rebuild_actions['inline hide-if-no-js'] = $actions['inline hide-if-no-js']; | ||
| $rebuild_actions['trash'] = $actions['trash']; | ||
| $rebuild_actions['view'] = $actions['view']; | ||
|
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. What about other plugins that add action links? I think an
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 definitely. |
||
|
|
||
| return $rebuild_actions; | ||
| } | ||
|
|
||
| /** | ||
| * 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(); | ||
| } | ||
|
|
||
| $id_param = ( 'page' === $post->post_type ) ? 'page_id' : 'p'; | ||
| $url = get_preview_post_link( $post, array(), home_url( '?preview=true&' . $id_param . '=' . $post->ID ) ); | ||
|
|
@@ -114,12 +163,8 @@ public function enqueue_admin_scripts() { | |
| ), | ||
| 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