|
5 | 5 |
|
6 | 6 | class Helpers extends Plugin { |
7 | 7 |
|
8 | | - /** |
9 | | - * Get the slug of the current page/post |
10 | | - * |
11 | | - * Return the slug of the current page/post. |
12 | | - * Example: http://mysite.com/sample-page/test-page would return: test-page |
13 | | - * |
14 | | - * @param int $post_id (optional) Specify the post ID to retrieve parent slug for, |
15 | | - * else $post global is used instead |
16 | | - * @return string The slug of the specified/current post |
17 | | - */ |
18 | | - public static function get_page_slug($post_id = null) { |
19 | | - global $post; |
| 8 | + /** |
| 9 | + * Display a notice/message in WP Admin |
| 10 | + * |
| 11 | + * @param string $msg The message to display. |
| 12 | + * @param string $type The type of notice. Valid values: |
| 13 | + * error, warning, success, info |
| 14 | + * @param bool $is_dismissible Specify whether or not the user may dismiss |
| 15 | + * the notice. |
| 16 | + * @return null |
| 17 | + */ |
| 18 | + public static function show_notice($msg, $type = 'error', $is_dismissible = false) { |
| 19 | + $class = 'notice notice-'.$type.($is_dismissible ? ' is-dismissible' : ''); |
| 20 | + $msg = __( $msg, self::$settings['data']['TextDomain'] ); |
20 | 21 |
|
21 | | - $_slug = $post_id ? get_post($post_id)->post_name : $post->post_name; |
| 22 | + printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $msg ); |
| 23 | + } |
22 | 24 |
|
23 | | - if(is_front_page()) { |
24 | | - $_slug = 'front'; |
25 | | - } else if(is_search()) { |
26 | | - $_slug = 'search'; |
27 | | - } else if(is_archive()) { |
28 | | - $_slug = 'archive'; |
29 | | - } else if(is_single()) { |
30 | | - $_slug = 'single'; |
31 | | - } |
| 25 | + /** |
| 26 | + * Get the slug of the current page/post |
| 27 | + * |
| 28 | + * Return the slug of the current page/post. |
| 29 | + * Example: http://mysite.com/sample-page/test-page would return: test-page |
| 30 | + * |
| 31 | + * @param int $post_id (optional) Specify the post ID to retrieve parent slug for, |
| 32 | + * else $post global is used instead |
| 33 | + * @return string The slug of the specified/current post |
| 34 | + */ |
| 35 | + public static function get_page_slug($post_id = null) { |
| 36 | + global $post; |
32 | 37 |
|
33 | | - return $_slug; |
34 | | - } |
| 38 | + $_slug = $post_id ? get_post($post_id)->post_name : $post->post_name; |
35 | 39 |
|
36 | | - /** |
37 | | - * Get the slug of the parent post (if any) |
38 | | - * |
39 | | - * Return the slug of the parent page/post. |
40 | | - * Example: http://mysite.com/sample-page/test-page would return: sample-page |
41 | | - * |
42 | | - * @param bool $include_self_as_parent_if_root (optional) Should we return the parent |
43 | | - * slug if the post *is* the parent? This can be useful if you want to apply |
44 | | - * style/logic to child pages as well as the parent |
45 | | - * @param int $post_id (optional) Specify the post ID to retrieve parent slug for, |
46 | | - * else $post global is used instead |
47 | | - * @return string The parent slug of the specified post, if availaable |
48 | | - */ |
49 | | - public static function get_parent_slug($include_self_as_parent_if_root = false, $post_id = null) { |
50 | | - global $post; |
51 | | - $post_id = $post_id ? $post_id : $post->ID; |
| 40 | + if(is_front_page()) { |
| 41 | + $_slug = 'front'; |
| 42 | + } else if(is_search()) { |
| 43 | + $_slug = 'search'; |
| 44 | + } else if(is_archive()) { |
| 45 | + $_slug = 'archive'; |
| 46 | + } else if(is_single()) { |
| 47 | + $_slug = 'single'; |
| 48 | + } |
52 | 49 |
|
53 | | - if (is_page()) { |
54 | | - if(get_post($post_id)->post_parent) { |
55 | | - @$parent = end(get_post_ancestors($post_id)); |
56 | | - } else { |
57 | | - $parent = $post->ID; |
58 | | - } |
59 | | - $post_data = get_post($parent, ARRAY_A); |
60 | | - if($include_self_as_parent_if_root || $post_data['post_name'] != self::get_page_slug($post_id)) return $post_data['post_name']; |
61 | | - } |
62 | | - return array(); |
63 | | - } |
| 50 | + return $_slug; |
| 51 | + } |
64 | 52 |
|
65 | | - /** |
66 | | - * Returns the categories of the current post |
67 | | - * |
68 | | - * Returns the categories of the current post, either as labels or as slugs. |
69 | | - * |
70 | | - * @param bool $as_slugs (optional) Returns array of category slugs rather than |
71 | | - * category labels |
72 | | - * @param int $post_id (optional) Specify the post ID to retrieve categories for, |
73 | | - * else $post global is used instead |
74 | | - * @return array |
75 | | - */ |
76 | | - public static function get_post_categories($as_slugs = false, $post_id = null) { |
77 | | - global $post; |
78 | | - $return = array(); |
| 53 | + /** |
| 54 | + * Get the slug of the parent post (if any) |
| 55 | + * |
| 56 | + * Return the slug of the parent page/post. |
| 57 | + * Example: http://mysite.com/sample-page/test-page would return: sample-page |
| 58 | + * |
| 59 | + * @param bool $include_self_as_parent_if_root (optional) Should we return the parent |
| 60 | + * slug if the post *is* the parent? This can be useful if you want to apply |
| 61 | + * style/logic to child pages as well as the parent |
| 62 | + * @param int $post_id (optional) Specify the post ID to retrieve parent slug for, |
| 63 | + * else $post global is used instead |
| 64 | + * @return string The parent slug of the specified post, if availaable |
| 65 | + */ |
| 66 | + public static function get_parent_slug($include_self_as_parent_if_root = false, $post_id = null) { |
| 67 | + global $post; |
| 68 | + $post_id = $post_id ? $post_id : $post->ID; |
79 | 69 |
|
80 | | - @$post_id = $post_id ? $post_id : $post->ID; |
81 | | - if(!$post_id) return $return; |
| 70 | + if (is_page()) { |
| 71 | + if(get_post($post_id)->post_parent) { |
| 72 | + @$parent = end(get_post_ancestors($post_id)); |
| 73 | + } else { |
| 74 | + $parent = $post->ID; |
| 75 | + } |
| 76 | + $post_data = get_post($parent, ARRAY_A); |
| 77 | + if($include_self_as_parent_if_root || $post_data['post_name'] != self::get_page_slug($post_id)) return $post_data['post_name']; |
| 78 | + } |
| 79 | + return array(); |
| 80 | + } |
82 | 81 |
|
83 | | - $categories = get_the_category($post_id); |
84 | | - if(!$categories) return $return; |
| 82 | + /** |
| 83 | + * Returns the categories of the current post |
| 84 | + * |
| 85 | + * Returns the categories of the current post, either as labels or as slugs. |
| 86 | + * |
| 87 | + * @param bool $as_slugs (optional) Returns array of category slugs rather than |
| 88 | + * category labels |
| 89 | + * @param int $post_id (optional) Specify the post ID to retrieve categories for, |
| 90 | + * else $post global is used instead |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + public static function get_post_categories($as_slugs = false, $post_id = null) { |
| 94 | + global $post; |
| 95 | + $return = array(); |
85 | 96 |
|
86 | | - foreach($categories as $cat) { |
87 | | - $return[] = $as_slugs ? $cat->slug : $cat->name; |
88 | | - } |
| 97 | + @$post_id = $post_id ? $post_id : $post->ID; |
| 98 | + if(!$post_id) return $return; |
89 | 99 |
|
90 | | - return $return; |
91 | | - } |
| 100 | + $categories = get_the_category($post_id); |
| 101 | + if(!$categories) return $return; |
| 102 | + |
| 103 | + foreach($categories as $cat) { |
| 104 | + $return[] = $as_slugs ? $cat->slug : $cat->name; |
| 105 | + } |
| 106 | + |
| 107 | + return $return; |
| 108 | + } |
92 | 109 |
|
93 | 110 | } |
0 commit comments