-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-contextual-help.php
143 lines (123 loc) · 4.24 KB
/
wp-contextual-help.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
Plugin Name: WP Contextual Help
Description: Allows a developer to easily extend the contextual help dropdown content area in WordPress
Version: 1.0.2
Author: kevinlangleyjr
Plugin URI: http://voceplatforms.com
*/
if( !class_exists( 'WP_Contextual_Help' ) ){
class WP_Contextual_Help {
static $help_docs_dir = array();
static $help_docs_img_url = array();
static $tabs = array();
static $tabs_by_page = array();
/**
* Initialize plugin
* @return void
*/
static function init(){
global $pagenow;
self::$help_docs_dir[] = get_template_directory() . '/includes/help-docs';
self::$help_docs_img_url[] = get_template_directory_uri() . '/includes/help-docs/img';
self::$help_docs_dir = apply_filters( 'wp_contextual_help_docs_dir', self::$help_docs_dir );
self::$help_docs_img_url = apply_filters( 'wp_contextual_help_docs_url', self::$help_docs_img_url );
foreach( self::$tabs as $tab ){
foreach( (array) $tab['page'] as $page ){
self::$tabs_by_page[ $page ][] = $tab;
if ( count( self::$tabs_by_page[ $page ] ) < 2 ) {
add_action( 'load-' . $page, array( __CLASS__, 'add_tab_to_screen' ) );
}
}
}
}
/**
* Registers new contextual help tab
* @param string $page Page path that is compared to the pagenow global
* @param string $id Help tab id
* @param string $title Help tab title
* @param array $args Arguments for the tab
*/
static function register_tab( $id, $title, $args ) {
if( empty( $args['page'] ) ){
_doing_it_wrong( __METHOD__, 'Cannot register help tab without specifying the page or pages to display it on', '0.0.1' );
}
$page = $args['page'];
unset( $args['page'] );
self::$tabs[$id] = array(
'page' => $page,
'id' => $id,
'title' => $title,
'args' => $args
);
}
/**
* Adds tabs to screen on 'load-' . $tab['page']
*
* Loops through all tabs and adds them on the appropriate screen
*/
static function add_tab_to_screen() {
$id = substr( current_action(), 5 );
$tabs = self::$tabs_by_page[ $id ];
foreach ( ( array ) $tabs as $tab ) {
// if post type arg is set, check the post type - if not same return
if ( isset( $tab[ 'args' ][ 'post_type' ] ) ) {
if ( !self::is_current_post_type( $tab ) ) {
continue;
}
}
$callback = !empty( $tab[ 'args' ][ 'callback' ] ) ? $tab[ 'args' ][ 'callback' ] : array( __CLASS__, 'echo_tab_html' );
get_current_screen()->add_help_tab( array(
'id' => $tab[ 'id' ],
'title' => $tab[ 'title' ],
'callback' => $callback
) );
}
}
/**
* Loads the tab html
* @param WP_Screen $screen Screen that the help tab is being added to
* @param array $screen_tab Screen tab array
* @return void
*/
static function echo_tab_html( $screen, $screen_tab ){
$content = '';
$tab = self::$tabs[ $screen_tab[ 'id' ] ];
$file_name = !empty( $tab[ 'file' ] ) ? $tab[ 'file' ] : $tab[ 'id' ] . '.html';
for( $i = 0; $i < count(self::$help_docs_dir); $i++ ){
$file = self::$help_docs_dir[$i] . DIRECTORY_SEPARATOR . $file_name;
if ( file_exists( $file ) ) {
if ( !empty( $tab[ 'args' ][ 'wpautop' ] ) ) {
$content = wpautop( file_get_contents( $file ) );
} else {
$content = file_get_contents( $file );
}
break;
}
}
if ( empty( $content ) ) {
$content = 'The provided HTML file is invalid or not founded.';
}
$content = str_replace( '{WP_HELP_IMG_URL}', self::$help_docs_img_url[$i], $content );
echo $content;
}
/**
* Checks to see if the current screen's post type is equivelent to the post type parameter provided when registering help tab
* @param array $tab
* @return boolean
*/
static function is_current_post_type( $tab ){
$screen = get_current_screen();
$current_post_type = isset( $screen->post_type ) ? $screen->post_type : false;
if( !empty( $current_post_type ) && isset( $tab['args']['post_type'] ) ){
if( is_array( $tab['args']['post_type'] ) ){
return in_array( $current_post_type, $tab['args']['post_type'] );
} else {
return (bool) ( $tab['args']['post_type'] == $current_post_type );
}
}
return false;
}
}
add_action( 'init', array( 'WP_Contextual_Help', 'init' ), 99 );
}