views
2 months ago
AdminMenu.php
3 weeks ago
EditWithButton.php
3 weeks ago
PostActions.php
2 months ago
EditWithButton.php
138 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EditWithButton show on post or custom post type edit page in wp admin page |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Admin; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\HelperFunctions; |
| 15 | use Kirki\Admin\AdminMenu; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * EditWithButton Class |
| 20 | */ |
| 21 | class EditWithButton { |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * Initialize the class |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | if ( HelperFunctions::user_has_editor_access() ) { |
| 31 | add_filter( 'post_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 ); |
| 32 | add_filter( 'page_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 ); |
| 33 | add_action( 'admin_enqueue_scripts', array( $this, 'add_custom_button_click_functionality' ), 10, 1 ); |
| 34 | add_action( 'admin_bar_menu', array( $this, 'add_edit_with_kirki_button' ), 100 ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function add_edit_with_kirki_button( $wp_admin_bar ) { |
| 39 | global $post; |
| 40 | |
| 41 | // Ensure we are on the front end and there's a valid post |
| 42 | if ( is_admin_bar_showing() && is_singular() && isset( $post->ID ) ) { |
| 43 | |
| 44 | // Check if the editor mode is set to "Kirki" and if the user has edit access |
| 45 | $editor_mode = HelperFunctions::is_editor_mode_is_kirki( $post->ID ); |
| 46 | if ( $editor_mode && HelperFunctions::user_has_post_edit_access( $post->ID ) ) { |
| 47 | |
| 48 | // Fetch the post URLs using your existing helper functions |
| 49 | $post_url_arr = HelperFunctions::get_post_url_arr_from_post_id( |
| 50 | $post->ID, |
| 51 | array( |
| 52 | 'editor_url' => true, |
| 53 | 'ajax_url' => true, |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | // Add the custom button to the admin bar |
| 58 | $wp_admin_bar->add_node( |
| 59 | array( |
| 60 | 'id' => 'edit_with_kirki', // Unique ID for the button |
| 61 | 'title' => '<span class="dashicons-kirki" style="padding: 10px;"></span>  ' . esc_html__( 'Edit with Kirki', 'kirki' ), // Button content |
| 62 | 'href' => $post_url_arr['editor_url'], // URL to the XYZ editor |
| 63 | 'ajaxUrl' => $post_url_arr['ajax_url'], |
| 64 | 'action' => KIRKI_EDITOR_ACTION, |
| 65 | 'isEditorModeIsKirki' => $editor_mode, |
| 66 | 'postId' => get_the_ID(), |
| 67 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | AdminMenu::add_kirki_admin_styles(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Filter Post Row Actions. |
| 79 | * |
| 80 | * Let the Document to filter the array of row action links on the Posts list table. |
| 81 | * |
| 82 | * @param array $actions wp actions. |
| 83 | * @param \WP_Post $post wp post. |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | public function filter_post_row_actions( $actions, $post ) { |
| 88 | $editor_mode = HelperFunctions::is_editor_mode_is_kirki( $post->ID ); |
| 89 | if ( $editor_mode && HelperFunctions::user_has_post_edit_access( $post->ID ) ) { |
| 90 | $editor_url = HelperFunctions::get_post_url_arr_from_post_id( $post->ID, array( 'editor_url' => true ) )['editor_url']; |
| 91 | $actions[ 'edit_with_' . 'kirki' ] = sprintf( |
| 92 | '<a href="%1$s" style="color:#9353FF;">%2$s</a>', |
| 93 | $editor_url, |
| 94 | esc_html__( 'Edit with Kirki', 'kirki' ) |
| 95 | ); |
| 96 | } |
| 97 | return $actions; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Add custom link in toolbar and click functionality |
| 102 | * |
| 103 | * @param string $hook wp hook or page name. |
| 104 | * |
| 105 | * @return void |
| 106 | */ |
| 107 | public function add_custom_button_click_functionality( $hook ) { |
| 108 | global $post; |
| 109 | |
| 110 | if ( 'post-new.php' === $hook || 'post.php' === $hook ) { |
| 111 | $version = KIRKI_VERSION; |
| 112 | $editor_mode = HelperFunctions::is_editor_mode_is_kirki( get_the_ID() ); |
| 113 | $post_url_arr = HelperFunctions::get_post_url_arr_from_post_id( |
| 114 | get_the_ID(), |
| 115 | array( |
| 116 | 'editor_url' => true, |
| 117 | 'ajax_url' => true, |
| 118 | 'rest_url' => true, |
| 119 | ) |
| 120 | ); |
| 121 | wp_enqueue_script( 'custom_link', KIRKI_ASSETS_URL . 'js/admin/custom-link-in-toolbar.js', array(), $version, true ); |
| 122 | wp_localize_script( |
| 123 | 'custom_link', |
| 124 | 'kirki_admin', |
| 125 | array( |
| 126 | 'postEditURL' => $post_url_arr['editor_url'], |
| 127 | 'ajaxUrl' => $post_url_arr['ajax_url'], |
| 128 | 'restUrl' => $post_url_arr['rest_url'], |
| 129 | 'action' => KIRKI_EDITOR_ACTION, |
| 130 | 'isEditorModeIsKirki' => $editor_mode, |
| 131 | 'postId' => get_the_ID(), |
| 132 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 |