5 | ```
6 |
7 | Creates a skeleton widget and shortcode for your plugin.
8 |
--------------------------------------------------------------------------------
/widget/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 | var base = require('../plugin-wp-base');
3 |
4 | module.exports = base.extend({
5 | constructor: function () {
6 | base.apply(this, arguments);
7 |
8 | this.argument('name', {
9 | required: false,
10 | type : String,
11 | desc : 'The widget name'
12 | });
13 | },
14 |
15 | initializing: {
16 | intro: function () {
17 | // Have Yeoman greet the user.
18 | this.log('Welcome to the neat Plugin WP Widget subgenerator!');
19 | },
20 |
21 | readingYORC: function() {
22 | this.rc = this.config.getAll() || {};
23 | },
24 |
25 | readingPackage: function() {
26 | this.pkg = this.fs.readJSON( this.destinationPath('package.json')) || {};
27 | },
28 |
29 | settingValues: function() {
30 | this.version = this.pkg.version;
31 | if ( this.name ) {
32 | this.name = this._.titleize( this.name.split('-').join(' ') );
33 | this.nameslug = this._.slugify( this.name );
34 | }
35 | this.pluginname = this.rc.name;
36 | this.widgetname = this.pluginname + ' ' + this._.capitalize( this.name );
37 | this.classname = this.rc.classprefix + this._wpClassify( this.name );
38 | this.mainclassname = this._wpClassify( this.pluginname );
39 | this.slug = this.rc.slug;
40 | this.classslug = this._.underscored( this._wpClassify( this.name ) );
41 | this.widgetslug = this.slug + '-' + this._.slugify( this.name );
42 | this.widgetregister = this._.underscored( this.slug + ' register ' + this.name );
43 | }
44 | },
45 |
46 | prompting: function () {
47 | var done = this.async();
48 |
49 | var prompts = [];
50 |
51 | if ( !this.version ) {
52 | prompts.push({
53 | type : 'input',
54 | name : 'version',
55 | message: 'Version',
56 | default: '0.1.0'
57 | });
58 | }
59 |
60 | if ( !this.name ) {
61 | prompts.push({
62 | type : 'input',
63 | name : 'name',
64 | message: 'Widget Name',
65 | default: 'basic-widget'
66 | });
67 | }
68 |
69 | if ( !this.pluginname ) {
70 | prompts.push({
71 | type : 'input',
72 | name : 'pluginname',
73 | message: 'Plugin Name',
74 | default: 'WDS Client Plugin'
75 | });
76 | }
77 |
78 | if ( prompts.length > 0 ) {
79 | this.log( 'Missing some info about the original plugin, help me out?' );
80 | this.prompt(prompts, function (props) {
81 | if ( props.version ) {
82 | this.version = props.version;
83 | }
84 |
85 | if ( props.name ) {
86 | this.name = this._.titleize( props.name.split('-').join(' ') );
87 | this.nameslug = this._.slugify( this.name );
88 | }
89 |
90 | if ( props.pluginname ) {
91 | this.pluginname = props.pluginname;
92 | this.slug = this._.slugify( props.pluginname );
93 | }
94 |
95 | if ( props.name || props.pluginname ) {
96 | this.widgetname = this.pluginname + ' ' + this._.capitalize( this.name );
97 | this.classname = this._wpClassPrefix( this.pluginname ) + this._wpClassify( this.name );
98 | this.widgetslug = this.slug + '-' + this._.slugify( this.name );
99 | this.widgetprefix = this._.underscored( this.slug + ' ' + this.name );
100 | }
101 |
102 | done();
103 | }.bind(this));
104 | } else {
105 | done();
106 | }
107 | },
108 |
109 | writing: function () {
110 | this.fs.copyTpl(
111 | this.templatePath('widget.php'),
112 | this.destinationPath('includes/class-' + this._.slugify( this.name ) + '.php'),
113 | this
114 | );
115 |
116 | if ( !this.rc.notests ) {
117 | this.fs.copyTpl(
118 | this.templatePath('tests.php'),
119 | this.destinationPath('tests/test-' + this._.slugify( this.name ) + '.php'),
120 | this
121 | );
122 | }
123 |
124 | this._addIncludeClass( this._.slugify( this.name ), this.classname, this.version );
125 | }
126 | });
127 |
--------------------------------------------------------------------------------
/widget/templates/tests.php:
--------------------------------------------------------------------------------
1 | Tests.
4 | *
5 | * @since <%= version %>
6 | * @package <%= mainclassname %>
7 | */
8 |
9 | /**
10 | * <%= widgetname %> Tests.
11 | *
12 | * @since <%= version %>
13 | */
14 | class <%= classname %>_Test extends WP_UnitTestCase { // @codingStandardsIgnoreLine
15 |
16 | /**
17 | * Test if our class exists.
18 | *
19 | * @since <%= version %>
20 | */
21 | public function test_class_exists() {
22 | $this->assertTrue( class_exists( '<%= classname %>' ) );
23 | }
24 |
25 | /**
26 | * Test that we can access our class through our helper function.
27 | *
28 | * @since <%= version %>
29 | */
30 | public function test_class_access() {
31 | $this->assertInstanceOf( '<%= classname %>', <%= rc.prefix %>()-><%= classslug %> ); // @codingStandardsIgnoreLine
32 | }
33 |
34 | /**
35 | * Replace this with some actual testing code.
36 | *
37 | * @since <%= version %>
38 | */
39 | public function test_sample() {
40 | $this->assertTrue( true );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/widget/templates/widget.php:
--------------------------------------------------------------------------------
1 | .
4 | *
5 | * @since <%= version %>
6 | * @package <%= mainclassname %>
7 | */
8 |
9 | /**
10 | * <%= widgetname %> class.
11 | *
12 | * @since <%= version %>
13 | */
14 | class <%= classname %> extends WP_Widget { // @codingStandardsIgnoreLine
15 |
16 | /**
17 | * Unique identifier for this widget.
18 | *
19 | * Will also serve as the widget class.
20 | *
21 | * @var string
22 | * @since <%= version %>
23 | */
24 | protected $widget_slug = '<%= widgetslug %>';
25 |
26 |
27 | /**
28 | * Widget name displayed in Widgets dashboard.
29 | * Set in __construct since __() shouldn't take a variable.
30 | *
31 | * @var string
32 | * @since <%= version %>
33 | */
34 | protected $widget_name = '';
35 |
36 |
37 | /**
38 | * Default widget title displayed in Widgets dashboard.
39 | * Set in __construct since __() shouldn't take a variable.
40 | *
41 | * @var string
42 | * @since <%= version %>
43 | */
44 | protected $default_widget_title = '';
45 |
46 | /**
47 | * Construct widget class.
48 | *
49 | * @since <%= version %>
50 | */
51 | public function __construct() {
52 |
53 | $this->widget_name = esc_html__( '<%= widgetname %>', '<%= slug %>' );
54 | $this->default_widget_title = esc_html__( '<%= widgetname %>', '<%= slug %>' );
55 |
56 | parent::__construct(
57 | $this->widget_slug,
58 | $this->widget_name,
59 | array(
60 | 'classname' => $this->widget_slug,
61 | 'description' => esc_html__( 'A widget boilerplate description.', '<%= slug %>' ),
62 | )
63 | );
64 |
65 | // Clear cache on save.
66 | add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
67 | add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
68 | add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
69 | }
70 |
71 | /**
72 | * Delete this widget's cache.
73 | *
74 | * Note: Could also delete any transients
75 | * delete_transient( 'some-transient-generated-by-this-widget' );
76 | *
77 | * @since <%= version %>
78 | */
79 | public function flush_widget_cache() {
80 | wp_cache_delete( $this->widget_slug, 'widget' );
81 | }
82 |
83 | /**
84 | * Front-end display of widget.
85 | *
86 | * @since <%= version %>
87 | *
88 | * @param array $args The widget arguments set up when a sidebar is registered.
89 | * @param array $instance The widget settings as set by user.
90 | */
91 | public function widget( $args, $instance ) {
92 |
93 | // Set widget attributes.
94 | $atts = array(
95 | 'before_widget' => $args['before_widget'],
96 | 'after_widget' => $args['after_widget'],
97 | 'before_title' => $args['before_title'],
98 | 'after_title' => $args['after_title'],
99 | 'title' => $instance['title'],
100 | 'text' => $instance['text'],
101 | );
102 |
103 | // Display the widget.
104 | echo $this->get_widget( $atts ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
105 | }
106 |
107 | /**
108 | * Return the widget output
109 | *
110 | * @since <%= version %>
111 | *
112 | * @param array $atts Array of widget args.
113 | * @return string Widget output
114 | */
115 | public function get_widget( $atts ) {
116 |
117 | $defaults = array(
118 | 'before_widget' => '',
119 | 'after_widget' => '',
120 | 'before_title' => '',
121 | 'after_title' => '',
122 | 'title' => '',
123 | 'text' => '',
124 | );
125 |
126 | // Start an output buffer.
127 | ob_start();
128 |
129 | // Start widget markup.
130 | echo $atts['before_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
131 |
132 | // Maybe display widget title.
133 | echo ( $atts['title'] ) ? $atts['before_title'] . esc_html( $atts['title'] ) . $atts['after_title'] : ''; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
134 |
135 | // Display widget text.
136 | echo wpautop( wp_kses_post( $atts['text'] ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
137 |
138 | // End the widget markup.
139 | echo $atts['after_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
140 |
141 | // Return the output buffer.
142 | return ob_get_clean();
143 | }
144 |
145 | /**
146 | * Update form values as they are saved.
147 | *
148 | * @since <%= version %>
149 | *
150 | * @param array $new_instance New settings for this instance as input by the user.
151 | * @param array $old_instance Old settings for this instance.
152 | * @return array Settings to save or bool false to cancel saving.
153 | */
154 | public function update( $new_instance, $old_instance ) {
155 |
156 | // Previously saved values.
157 | $instance = $old_instance;
158 |
159 | // Sanity check new data existing.
160 | $title = isset( $new_instance['title'] ) ? $new_instance['title'] : '';
161 | $text = isset( $new_instance['text'] ) ? $new_instance['text'] : '';
162 |
163 | // Sanitize title before saving to database.
164 | $instance['title'] = sanitize_text_field( $title );
165 |
166 | // Sanitize text before saving to database.
167 | if ( current_user_can( 'unfiltered_html' ) ) {
168 | $instance['text'] = force_balance_tags( $text );
169 | } else {
170 | $instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $text ) ) );
171 | }
172 |
173 | // Flush cache.
174 | $this->flush_widget_cache();
175 |
176 | return $instance;
177 | }
178 |
179 | /**
180 | * Back-end widget form with defaults.
181 | *
182 | * @since <%= version %>
183 | *
184 | * @param array $instance Current settings.
185 | */
186 | public function form( $instance ) {
187 |
188 | // Set defaults.
189 | $defaults = array(
190 | 'title' => $this->default_widget_title,
191 | 'text' => '',
192 | );
193 |
194 | // Parse args.
195 | $instance = wp_parse_args( (array) $instance, $defaults );
196 | ?>
197 |
198 |
201 |
202 |
203 |
204 |
207 |
208 |
209 |
210 | ' ); ?>
211 |
212 | () { // @codingStandardsIgnoreLine
220 | register_widget( '<%= classname %>' );
221 | }
222 | add_action( 'widgets_init', '<%= widgetregister %>' );
223 |
--------------------------------------------------------------------------------