-
Notifications
You must be signed in to change notification settings - Fork 64
/
plugin-wp-base.js
109 lines (82 loc) · 3.15 KB
/
plugin-wp-base.js
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
'use strict';
var yeoman = require('yeoman-generator');
var updateNotifier = require('update-notifier');
module.exports = yeoman.generators.Base.extend({
constructor: function () {
// Calling the super constructor is important so our generator is correctly set up
yeoman.generators.Base.apply(this, arguments);
updateNotifier({
pkg: require('./package.json')
}).notify({defer: false});
},
_wpClassify: function( s ) {
var words = this._.words( s );
var result = '';
for ( var i = 0; i < words.length; i += 1 ) {
if ( this._.classify( words[i] ) ) {
result += this._.capitalize( words[i] );
if ( (i + 1) < words.length ) {
result += '_';
}
}
}
result = result.replace('-', '_');
return result;
},
_wpClassPrefix: function( s ) {
var words = s.replace( /_/g, ' ' );
var letters = words.replace(/[a-z]/g, '');
var hyphens = letters.replace(/\s/g, '');
var prefix = hyphens.replace(/-/g,'');
return prefix + '_';
},
_escapeDoubleQuotes: function( s ) {
return s.replace( /"/g, '\\"');
},
__addStringToPluginClasses: function( mainPluginFile, toAdd ) {
var endComment = '\t} // END OF PLUGIN CLASSES FUNCTION';
var newInclude = '\t\t' + toAdd + '\n' + endComment;
return mainPluginFile.replace( endComment, newInclude );
},
_addStringToPluginClasses: function( toAdd ) {
if ( ! this.rc.slug ) {
return;
}
var mainPluginFile = this.fs.read( this.destinationPath( this.rc.slug + '.php' ) );
mainPluginFile = this.__addStringToPluginClasses( mainPluginFile, toAdd );
this.fs.write( this.destinationPath( this.rc.slug + '.php' ), mainPluginFile );
},
_addPluginProperty: function( file, slug, className, version ) {
var toAdd = '\t/**';
toAdd += '\n\t * Instance of ' + className;
toAdd += '\n\t *';
toAdd += '\n\t * @since' + version;
toAdd += '\n\t * @var ' + className;
toAdd += '\n\t */';
toAdd += '\n\tprotected $' + slug + ';';
var endComment = '\t/**\n\t * Creates or returns an instance of this class.';
return file.replace( endComment, toAdd + '\n\n' + endComment );
},
_addPluginClass: function( file, slug, className ) {
var toAdd = '$this->' + slug + ' = new ' + className + '( $this );';
var toRemove = '\n\t\t// $this->plugin_class = new ' + this.rc.classprefix + 'Plugin_Class( $this );';
return this.__addStringToPluginClasses( file.replace( toRemove, '' ), toAdd );
},
_addPropertyMagicGetter: function( file, slug ) {
var toAdd = '\t\t\tcase \'' + slug + '\':';
var endComment = '\t\t\t\treturn $this->$field;';
var newInclude = toAdd + '\n' + endComment;
return file.replace( endComment, newInclude );
},
_addIncludeClass: function( slug, className, version ) {
if ( ! this.rc.slug ) {
return;
}
slug = this._.underscored( slug );
var mainPluginFile = this.fs.read( this.destinationPath( this.rc.slug + '.php' ) );
mainPluginFile = this._addPluginProperty( mainPluginFile, slug, className, version );
mainPluginFile = this._addPluginClass( mainPluginFile, slug, className );
mainPluginFile = this._addPropertyMagicGetter( mainPluginFile, slug );
this.fs.write( this.destinationPath( this.rc.slug + '.php' ), mainPluginFile );
}
});