Skip to content

Commit 175590e

Browse files
committed
Updated for CF 2.0.3
1 parent 3cbc3dc commit 175590e

5 files changed

Lines changed: 137 additions & 87 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ It may also be used as the means of [separating custom code](http://www.billeric
1717

1818
## Change Log
1919

20+
#### 0.2.0
21+
22+
* Removed `./vendor` from repo.
23+
* Added [wpupdatephp/wp-update-php](https://github.com/WPupdatePHP/wp-update-php) as dependency
24+
2025
#### 0.1.2
2126

2227
* Moved `/src` to `/app`
@@ -26,7 +31,7 @@ It may also be used as the means of [separating custom code](http://www.billeric
2631
* Refactored code
2732
* Added `is_production()` and `is_ajax()` methods
2833

29-
#### 0.1.0 - December 26, 2016
34+
#### 0.1.0
3035

3136
* Initial commit
3237

app/Helpers.php

Lines changed: 91 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,106 @@
55

66
class Helpers extends Plugin {
77

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'] );
2021

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+
}
2224

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;
3237

33-
return $_slug;
34-
}
38+
$_slug = $post_id ? get_post($post_id)->post_name : $post->post_name;
3539

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+
}
5249

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+
}
6452

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;
7969

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+
}
8281

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();
8596

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;
8999

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+
}
92109

93110
}

app/Plugin.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ function __construct($_settings) {
1414
self::$prefix = $_settings['prefix'];
1515
self::$settings = $_settings;
1616

17+
// Initialize Carbon Fields and Check Loded Version
18+
add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );
19+
add_action( 'carbon_fields_loaded', array($this, 'load_plugin') );
20+
21+
}
22+
23+
public function load_plugin() {
24+
25+
if(!$this->verify_dependencies()) return;
26+
1727
// Enqueue scripts
1828
new EnqueueScripts();
1929

@@ -34,6 +44,27 @@ function __construct($_settings) {
3444

3545
}
3646

47+
/**
48+
* Function to verify dependencies, such as if an outdated version of Carbon
49+
* Fields is detected.
50+
*
51+
* @return bool
52+
*/
53+
private function verify_dependencies() {
54+
55+
$error = null;
56+
57+
if(!defined('\\Carbon_Fields\\VERSION')) {
58+
$error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('A fatal error occurred while trying to load dependencies.');
59+
} else if( version_compare( \Carbon_Fields\VERSION, self::$settings['deps']['carbon_fields'], '<' ) ) {
60+
$error = '<strong>' . self::$settings['data']['Name'] . ':</strong> ' . __('Unable to load. An outdated version of Carbon Fields has been loaded:' . ' ' . \Carbon_Fields\VERSION) . ' (&gt;= '.self::$settings['deps']['carbon_fields'] . ' ' . __('required') . ')';
61+
}
62+
63+
if($error) Helpers::show_notice($error, 'error', false);
64+
return !$error;
65+
66+
}
67+
3768
/**
3869
* Returns true if WP_ENV is anything other than 'development' or 'staging'.
3970
* Useful for determining whether or not to enqueue a minified or non-
@@ -42,11 +73,7 @@ function __construct($_settings) {
4273
* @return bool
4374
*/
4475
public function is_production() {
45-
if( !defined('WP_ENV') ) {
46-
return true;
47-
} else {
48-
return !in_array(WP_ENV, ['development', 'staging']);
49-
}
76+
return ( !defined('WP_ENV') || (defined('WP_ENV') && !in_array(WP_ENV, ['development', 'staging']) ) );
5077
}
5178

5279
/**

app/WidgetLoader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class My_Widget_Class extends Widget {
2323

2424
function __construct() {
2525

26-
$this->setup(__('Plugin Widget - Example'), 'Displays a block with title/text', array(
27-
Field::make('text', WidgetLoader::$prefix.'widget_title', 'Title')->set_default_value('Hello World!'),
28-
Field::make('textarea', WidgetLoader::$prefix.'widget_content', 'Content')->set_default_value('Lorem Ipsum dolor sit amet')
29-
));
26+
$this->setup( WidgetLoader::$prefix . 'example_widget', __( 'Plugin Widget - Example', WidgetLoader::$textdomain ), 'Displays a block with title/text', array(
27+
Field::make( 'text', WidgetLoader::$prefix . 'widget_title', 'Title' )->set_default_value('Hello World!'),
28+
Field::make( 'textarea', WidgetLoader::$prefix . 'widget_content', 'Content' )->set_default_value('Lorem ipsum dolor sit amet')
29+
), '');
3030

3131
}
3232

3333
// Called when rendering the widget in the front-end
34-
function front_end($args, $instance) {
35-
echo $args['before_title'] . $instance[WidgetLoader::$prefix.'widget_title'] . $args['after_title'];
34+
function front_end( $args, $instance ) {
35+
echo $args['before_title'] . $instance[WidgetLoader::$prefix . 'widget_title'] . $args['after_title'];
3636
echo '<p>' . $instance[WidgetLoader::$prefix.'widget_content'] . '</p>';
3737
}
3838

wordpress-base-plugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: WordPress Base Plugin
55
* Plugin URI: https://github.com/dmhendricks/wordpress-base-plugin
66
* Description: A boilerplate for WordPress plugins
7-
* Version: 0.1.2
7+
* Version: 0.2.0
88
* Author: Daniel M. Hendricks
99
* Author URI: https://2lab.net
1010
* License: GPL-2.0
@@ -42,6 +42,7 @@
4242
'textdomain' => 'my-plugin',
4343
'object_cache_group' => 'my_plugin_cache',
4444
'object_cache_expire' => 72, // In hours
45+
'deps' => ['carbon_fields' => '2.0.0'],
4546
'prefix' => 'myplugin_' // Change to your own unique field prefix
4647
));
4748
?>

0 commit comments

Comments
 (0)