page-locations
3 months ago
admin-bar.php
3 months ago
controller.php
3 months ago
footer-text.php
3 months ago
function.php
3 months ago
wp-dashboard.php
3 months ago
admin-bar.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Bar |
| 4 | */ |
| 5 | |
| 6 | namespace PluginRx\AdminHelpDocs; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 9 | |
| 10 | class AdminBar { |
| 11 | |
| 12 | private static $trucated_title_length = 50; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * The single instance of the class |
| 17 | * |
| 18 | * @var self|null |
| 19 | */ |
| 20 | private static ?AdminBar $instance = null; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Get the singleton instance |
| 25 | * |
| 26 | * @return self |
| 27 | */ |
| 28 | public static function instance() : self { |
| 29 | return self::$instance ??= new self(); |
| 30 | } // End instance() |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | */ |
| 36 | private function __construct() { |
| 37 | self::$trucated_title_length = apply_filters( 'helpdocs_admin_bar_truncated_title_length', self::$trucated_title_length ); |
| 38 | |
| 39 | $do_backend = filter_var( get_option( 'helpdocs_admin_bar' ), FILTER_VALIDATE_BOOLEAN ) && is_admin(); |
| 40 | $do_frontend = filter_var( get_option( 'helpdocs_admin_bar_frontend' ), FILTER_VALIDATE_BOOLEAN ) && ! is_admin(); |
| 41 | |
| 42 | if ( ( $do_backend || $do_frontend ) && Helpers::user_can_view() ) { |
| 43 | add_action( 'admin_bar_menu', [ $this, 'admin_bar' ], 100 ); |
| 44 | } |
| 45 | } // End __construct() |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Customize Admin Bar |
| 50 | * |
| 51 | * @param object $wp_admin_bar |
| 52 | * @return void |
| 53 | */ |
| 54 | public function admin_bar( $wp_admin_bar ) { |
| 55 | $title = Helpers::get_menu_title(); |
| 56 | |
| 57 | $dashicon = Helpers::get_icon(); |
| 58 | |
| 59 | $parent_id = 'helpdocs_admin_bar'; |
| 60 | |
| 61 | $icon_html = '<span class="ab-icon ' . esc_attr( $dashicon ) . '" style="top: 2px;"></span>'; |
| 62 | $label_html = '<span class="screen-reader-text">' . esc_html( $title ) . '</span>'; |
| 63 | |
| 64 | $has_new_help_doc_link = Helpers::user_can_edit() && ! Helpers::is_our_screen() && is_admin(); |
| 65 | |
| 66 | $wp_admin_bar->add_node( [ |
| 67 | 'id' => $parent_id, |
| 68 | 'title' => $icon_html . $label_html, |
| 69 | 'href' => Bootstrap::tab_url( 'documentation' ), |
| 70 | 'meta' => [ |
| 71 | 'target' => '_blank', |
| 72 | 'title' => esc_attr( $title ), // This adds the native browser tooltip, |
| 73 | 'class' => $has_new_help_doc_link ? ' has-add-new-link' : '', |
| 74 | ], |
| 75 | ] ); |
| 76 | |
| 77 | $docs = Helpers::get_docs( [ |
| 78 | 'site_location' => 'admin_bar', |
| 79 | ] ); |
| 80 | if ( ! empty( $docs ) ) { |
| 81 | |
| 82 | $include_content = filter_var( get_option( 'helpdocs_admin_bar_include_content' ), FILTER_VALIDATE_BOOLEAN ); |
| 83 | |
| 84 | usort( $docs, function( $a, $b ) { return strcmp( $a->helpdocs_order, $b->helpdocs_order ) ; } ); |
| 85 | |
| 86 | foreach ( $docs as $key => $doc ) { |
| 87 | $content = get_the_excerpt( $doc ); |
| 88 | $href = filter_var( $content, FILTER_VALIDATE_URL ) ? $content : false; |
| 89 | $suffix = ''; |
| 90 | |
| 91 | if ( ! $href && '' !== $content && $include_content ) { |
| 92 | $suffix = ' — ' . esc_html( wp_html_excerpt( wp_strip_all_tags( $content ), self::$trucated_title_length, '...' ) ); |
| 93 | } |
| 94 | |
| 95 | // If no URL found in content, check for internal documentation link |
| 96 | if ( ! $href ) { |
| 97 | $locations = get_post_meta( $doc->ID, 'helpdocs_locations', true ); |
| 98 | if ( is_array( $locations ) ) { |
| 99 | foreach ( $locations as $loc ) { |
| 100 | if ( base64_encode( 'main' ) === ( $loc[ 'site_location' ] ?? '' ) ) { |
| 101 | $href = add_query_arg( 'id', absint( $doc->ID ), Bootstrap::tab_url( 'documentation' ) ); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $wp_admin_bar->add_node( [ |
| 109 | 'id' => 'helpdocs_' . $key, |
| 110 | 'parent' => $parent_id, |
| 111 | 'title' => esc_html( $doc->post_title ) . $suffix, |
| 112 | 'href' => $href, |
| 113 | 'meta' => [ 'target' => '_blank' ], |
| 114 | ] ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ( $has_new_help_doc_link ) { |
| 119 | global $pagenow; |
| 120 | $screen = get_current_screen(); |
| 121 | |
| 122 | $all_locations = HelpDocs::site_locations(); |
| 123 | |
| 124 | $relative_path = ( in_array( $pagenow, [ 'post.php', 'post-new.php' ] ) ) ? 'post.php' : ( ( $pagenow === 'edit.php' ) ? 'edit.php' : $pagenow ); |
| 125 | |
| 126 | $is_standard = array_key_exists( $relative_path, $all_locations ); |
| 127 | |
| 128 | $url_params = [ |
| 129 | 'post_type' => HelpDocs::$post_type, |
| 130 | 'slpt' => ( $screen ) ? $screen->post_type : '', |
| 131 | ]; |
| 132 | |
| 133 | if ( $is_standard ) { |
| 134 | $url_params[ 'site_location' ] = base64_encode( $relative_path ); |
| 135 | } else { |
| 136 | $url_params[ 'site_location' ] = base64_encode( 'custom' ); |
| 137 | |
| 138 | $clean_get = wp_unslash( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 139 | |
| 140 | unset( |
| 141 | $clean_get[ '_wp_http_referer' ], |
| 142 | $clean_get[ 'settings-updated' ], |
| 143 | $clean_get[ 'wp_http_referer' ] |
| 144 | ); |
| 145 | |
| 146 | $current_url = admin_url( $pagenow ); |
| 147 | |
| 148 | if ( ! empty( $clean_get ) ) { |
| 149 | $current_url = add_query_arg( map_deep( $clean_get, 'sanitize_text_field' ), $current_url ); |
| 150 | } |
| 151 | |
| 152 | $final_url = remove_query_arg( [ '_wp_http_referer', 'settings-updated' ], $current_url ); |
| 153 | |
| 154 | $url_params[ 'custom_url' ] = urlencode( $final_url ); |
| 155 | } |
| 156 | |
| 157 | $add_new_url = add_query_arg( $url_params, admin_url( 'post-new.php' ) ); |
| 158 | |
| 159 | $wp_admin_bar->add_node( [ |
| 160 | 'id' => 'helpdocs_add_new', |
| 161 | 'parent' => $parent_id, |
| 162 | 'title' => '<span class="ab-icon dashicons-plus-alt"></span>' . __( 'Add help doc to this page', 'admin-help-docs' ), |
| 163 | 'href' => $add_new_url, |
| 164 | 'meta' => [ 'target' => '_blank', 'class' => 'helpdocs-add-new-admin-bar' ], |
| 165 | ] ); |
| 166 | } |
| 167 | } // End admin_bar() |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Prevent cloning and unserializing |
| 172 | */ |
| 173 | public function __clone() {} |
| 174 | public function __wakeup() {} |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | AdminBar::instance(); |