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
wp-dashboard.php
262 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress Dashboard |
| 4 | */ |
| 5 | |
| 6 | namespace PluginRx\AdminHelpDocs; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 9 | |
| 10 | class WPDashboard { |
| 11 | |
| 12 | /** |
| 13 | * The single instance of the class |
| 14 | * |
| 15 | * @var self|null |
| 16 | */ |
| 17 | private static ?WPDashboard $instance = null; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Get the singleton instance |
| 22 | * |
| 23 | * @return self |
| 24 | */ |
| 25 | public static function instance() : self { |
| 26 | return self::$instance ??= new self(); |
| 27 | } // End instance() |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Constructor |
| 32 | */ |
| 33 | private function __construct() { |
| 34 | // Only proceed if we're on the dashboard and not already on our custom dashboard page |
| 35 | $is_custom_dashboard = isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] === 'admin-help-dashboard'; // phpcs:ignore |
| 36 | if ( $is_custom_dashboard ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Add dashboard widgets for each doc with a site location of index.php |
| 41 | add_action( 'wp_dashboard_setup', [ $this, 'doc_widgets' ] ); |
| 42 | |
| 43 | // Table of Contents widget |
| 44 | if ( get_option( 'helpdocs_dashboard_toc' ) ) { |
| 45 | add_action( 'wp_dashboard_setup', [ $this, 'toc_widget' ] ); |
| 46 | } |
| 47 | } // End __construct() |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * The single rendering point for the custom dashboard page |
| 52 | */ |
| 53 | public static function render_replacement_page() : void { |
| 54 | if ( ! get_option( 'helpdocs_replace_dashboard' ) ) { |
| 55 | wp_safe_redirect( admin_url() ); |
| 56 | exit; |
| 57 | } |
| 58 | $docs = Helpers::get_docs( [ |
| 59 | 'site_location' => 'replace_dashboard', |
| 60 | ] ); |
| 61 | |
| 62 | $doing_toc = false; |
| 63 | ?> |
| 64 | <div id="<?php echo esc_attr( Bootstrap::textdomain() ); ?>" class="wrap"> |
| 65 | <?php |
| 66 | if ( get_option( 'helpdocs_dashboard_toc' ) && Helpers::user_can_view() ) { |
| 67 | $doing_toc = true; |
| 68 | ?> |
| 69 | <div id="helpdocs-dashboard-toc"> |
| 70 | <?php self::instance()->toc_widget_content(); ?> |
| 71 | </div> |
| 72 | <?php |
| 73 | } |
| 74 | ?> |
| 75 | <div id="helpdocs-custom-dashboard"> |
| 76 | <?php if ( ! empty( $docs ) ) : ?> |
| 77 | <?php foreach ( $docs as $doc ) : ?> |
| 78 | <div class="helpdocs-box" data-doc-id="<?php echo esc_attr( $doc->ID ); ?>"> |
| 79 | <?php |
| 80 | if ( Helpers::user_can_edit() ) { |
| 81 | $edit_url = get_edit_post_link( $doc->ID ); |
| 82 | $incl_edit = ' <span class="edit-link"><a href="' . esc_url( $edit_url ) . '">✎ ' . __( 'Edit', 'admin-help-docs' ) . '</a></span>'; |
| 83 | } else { |
| 84 | $incl_edit = ''; |
| 85 | } |
| 86 | $post_content = $doc->post_content; |
| 87 | ?> |
| 88 | <div class="helpdoc-header"> |
| 89 | <h2><?php echo wp_kses_post( Helpers::convert_merge_tags( $doc->post_title ) ); ?></h2><?php echo wp_kses_post( $incl_edit ); ?> |
| 90 | </div> |
| 91 | <div class="helpdoc-content"><?php |
| 92 | $allowed_tags = wp_kses_allowed_html( 'post' ); |
| 93 | $allowed_tags = Helpers::allow_addt_tags( $allowed_tags ); |
| 94 | |
| 95 | echo wp_kses( apply_filters( 'the_content', Helpers::convert_merge_tags( $post_content ) ), $allowed_tags ); |
| 96 | ?></div> |
| 97 | </div> |
| 98 | <?php endforeach; ?> |
| 99 | |
| 100 | <?php elseif ( ! $doing_toc ) : ?> |
| 101 | <?php |
| 102 | /* translators: Welcome, name! */ |
| 103 | $welcome_text = sprintf( __( 'Welcome, %s!', 'admin-help-docs' ), '{first_name}' ); |
| 104 | ?> |
| 105 | <div id="dashboard-no-docs"> |
| 106 | <h1><?php echo wp_kses_post( Helpers::convert_merge_tags( $welcome_text ) ); ?></h1> |
| 107 | <p><?php |
| 108 | $no_content_text = __( 'Everything you need is in the left menu.', 'admin-help-docs' ); |
| 109 | echo wp_kses_post( apply_filters( 'helpdocs_dashboard_no_docs_text', $no_content_text ) ); |
| 110 | if ( Helpers::user_can_edit() ) { |
| 111 | $create_doc_url = Bootstrap::tab_url( 'manage' ); |
| 112 | echo '<br>'; |
| 113 | printf( |
| 114 | wp_kses( |
| 115 | /* translators: %1$s: URL to the Manage Docs screen. */ |
| 116 | __( 'To add content here, <a href="%1$s">create a doc</a> and set its location to "WordPress Dashboard (Replaces Dashboard Entirely)".', 'admin-help-docs' ), |
| 117 | [ 'a' => [ 'href' => [] ] ] |
| 118 | ), |
| 119 | esc_url( $create_doc_url ) |
| 120 | ); |
| 121 | } |
| 122 | ?></p> |
| 123 | </div> |
| 124 | <?php endif; ?> |
| 125 | </div> |
| 126 | </div> |
| 127 | <?php |
| 128 | } // End render_replacement_page() |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Dashboard content callback |
| 133 | * |
| 134 | * @param WP_Post $post The post object for the widget |
| 135 | */ |
| 136 | public function doc_widgets() { |
| 137 | $docs = Helpers::get_docs( [ |
| 138 | 'site_location' => 'index.php', |
| 139 | ] ); |
| 140 | if ( ! empty( $docs ) ) { |
| 141 | |
| 142 | $user_can_edit = Helpers::user_can_edit(); |
| 143 | |
| 144 | foreach ( $docs as $doc ) { |
| 145 | if ( ! Helpers::user_can_view( $doc->ID ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | if ( $user_can_edit ) { |
| 150 | if ( isset( $doc->feed_id ) && $doc->feed_id != '' ) { |
| 151 | $post_id = $doc->feed_id; |
| 152 | } else { |
| 153 | $post_id = $doc->ID; |
| 154 | } |
| 155 | $url = add_query_arg( [ |
| 156 | 'post' => absint( $post_id ), |
| 157 | 'action' => 'edit', |
| 158 | ], admin_url( 'post.php' ) ); |
| 159 | |
| 160 | $incl_edit = '<span class="helpdocs-dashboard-widget-link"> |
| 161 | <a href="' . $url . '">✎ ' . __( 'Edit', 'admin-help-docs' ) . '</a> |
| 162 | </span>'; |
| 163 | } else { |
| 164 | $incl_edit = ''; |
| 165 | } |
| 166 | |
| 167 | $title = $doc->post_title . $incl_edit; |
| 168 | |
| 169 | wp_add_dashboard_widget( 'helpdocs_' . absint( $doc->ID ), $title, [ $this, 'doc_widget_content' ], null, $doc, 'normal', 'high' ); |
| 170 | } |
| 171 | } |
| 172 | } // End doc_widgets() |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * Dashboard content |
| 177 | * |
| 178 | * @return void |
| 179 | */ |
| 180 | public function doc_widget_content( $var, $args ) { |
| 181 | $doc = $args[ 'args' ]; |
| 182 | echo wp_kses_post( $doc->post_content ); |
| 183 | } // End doc_widget_content() |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Table of Contents widget |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | public function toc_widget() { |
| 192 | $view_all_docs_link = '<span class="helpdocs-dashboard-widget-link"> |
| 193 | <a href="' . Bootstrap::tab_url( 'documentation' ) . '">' . __( 'View All Docs', 'admin-help-docs' ) . '</a> |
| 194 | </span>'; |
| 195 | |
| 196 | $title = Helpers::get_menu_title() . ' ' . $view_all_docs_link; |
| 197 | |
| 198 | if ( Helpers::user_can_view() ) { |
| 199 | wp_add_dashboard_widget( Bootstrap::textdomain(), $title, [ $this, 'toc_widget_content' ], null, null, 'normal', 'high' ); |
| 200 | } |
| 201 | } // End toc_widget() |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Table of Contents content |
| 206 | * |
| 207 | * @return void |
| 208 | */ |
| 209 | public function toc_widget_content() { |
| 210 | $docs = Helpers::get_docs( [ |
| 211 | 'site_location' => 'main', |
| 212 | 'toc' => true, |
| 213 | ] ); |
| 214 | |
| 215 | if ( ! empty( $docs ) ) { |
| 216 | usort( $docs, function( $a, $b ) { return strcmp( $a->helpdocs_order, $b->helpdocs_order ) ; } ); |
| 217 | |
| 218 | $results = '<div class="toc-cont"><ul>'; |
| 219 | |
| 220 | foreach ( $docs as $doc ) { |
| 221 | $toc_mk = 'helpdocs_toc'; |
| 222 | if ( isset( $doc->$toc_mk ) && !$doc->$toc_mk ) { |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | $link_params = [ 'id' => absint( $doc->ID ) ]; |
| 227 | |
| 228 | if ( isset( $doc->auto_feed ) && $doc->auto_feed != '' ) { |
| 229 | $link_params[ 'feed' ] = 'true'; |
| 230 | $icon = 'dashicons-cloud'; |
| 231 | } else { |
| 232 | $icon = Helpers::get_icon(); |
| 233 | } |
| 234 | if ( ! str_starts_with( $icon, 'dashicons-' ) ) { |
| 235 | $icon = 'dashicons-' . $icon; |
| 236 | } |
| 237 | |
| 238 | $link = add_query_arg( $link_params, Bootstrap::tab_url( 'documentation' ) ); |
| 239 | $title = $doc->post_title; |
| 240 | |
| 241 | $results .= '<li><a class="toc-item" href="' . $link . '"><span class="dashicons ' . $icon . '"></span> ' . $title . '</a></li>'; |
| 242 | } |
| 243 | |
| 244 | $results .= '</ul></div>'; |
| 245 | echo wp_kses_post( $results ); |
| 246 | |
| 247 | } else { |
| 248 | echo esc_html__( 'You have not added any help docs. To do so, edit the docs you want to edit and choose "Add to Dashboard Table of Contents" under "Location" settings.', 'admin-help-docs' ); |
| 249 | } |
| 250 | } // End toc_widget_content() |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * Prevent cloning and unserializing |
| 255 | */ |
| 256 | public function __clone() {} |
| 257 | public function __wakeup() {} |
| 258 | |
| 259 | } |
| 260 | |
| 261 | |
| 262 | WPDashboard::instance(); |