js
3 months ago
bottom.php
3 months ago
contextual.php
3 months ago
element.php
3 months ago
side.php
3 months ago
top.php
3 months ago
contextual.php
82 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Contextual Help |
| 4 | */ |
| 5 | |
| 6 | namespace PluginRx\AdminHelpDocs; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 9 | |
| 10 | class Contextual { |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * The docs to be rendered |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | private array $docs = []; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * Constructor |
| 23 | */ |
| 24 | public function __construct( $docs = [] ) { |
| 25 | if ( empty( $docs ) ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | $this->docs = $docs; |
| 30 | |
| 31 | add_action( 'admin_head', [ $this, 'add_contextual_help_tabs' ] ); |
| 32 | add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] ); |
| 33 | } // End __construct() |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Register help tabs for the current screen on non-gutenberg pages |
| 38 | */ |
| 39 | public function add_contextual_help_tabs() : void { |
| 40 | $screen = get_current_screen(); |
| 41 | if ( ! $screen || Helpers::is_gutenberg() || empty( $this->docs ) ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | foreach ( $this->docs as $doc ) { |
| 46 | $screen->add_help_tab( [ |
| 47 | 'id' => 'help_tab_' . $doc->ID, |
| 48 | 'title' => $doc->post_title, |
| 49 | 'content' => apply_filters( 'the_content', $doc->post_content ), |
| 50 | ] ); |
| 51 | } |
| 52 | } // End add_contextual_help_tabs() |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Add a help tab button for the current gutenberg screen |
| 57 | */ |
| 58 | public function scripts() : void { |
| 59 | $screen = get_current_screen(); |
| 60 | if ( ! $screen || ! Helpers::is_gutenberg() || empty( $this->docs ) ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $docs = Helpers::clean_docs_for_gutenberg( $this->docs ); |
| 65 | |
| 66 | $text_domain = Bootstrap::textdomain(); |
| 67 | $script_version = Bootstrap::script_version(); |
| 68 | |
| 69 | wp_enqueue_script( $text_domain . "-contextual", Bootstrap::url( "inc/docs/page-locations/js/contextual.js" ), [ 'jquery' ], $script_version, true ); |
| 70 | wp_localize_script( $text_domain . "-contextual", "helpdocs_contextual", [ |
| 71 | 'docs' => $docs, |
| 72 | ] ); |
| 73 | } // End scripts() |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Prevent cloning and unserializing |
| 78 | */ |
| 79 | public function __clone() {} |
| 80 | public function __wakeup() {} |
| 81 | |
| 82 | } |