-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpcampus-build.php
189 lines (172 loc) · 4.6 KB
/
wpcampus-build.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
## initial command, slide 10
if ( ! defined('WP_CLI') ) {
return;
}
if (!class_exists('WP_CLI')) {
return;
}
WP_CLI::add_command('wpcampus',function (){
echo "Welcome to WPCampus!!!",PHP_EOL;
});
## docs, slide 22
/**
* Welcomes a user to WPCampus 2023
*
* ## EXAMPLES
*
* # Welcome a user
* $ wp wpcampus
* Welcome to WPCampus!!!
*/
## adds our positional argument, slide 29
WP_CLI::add_command('wpcampus',function ($args, $assoc_args){
printf("Welcome to WPCampus%s!!!" . PHP_EOL, ($args[0]) ? ", ${args[0]}" : '');
});
## adds our associative argument, slide 30
WP_CLI::add_command('wpcampus',function ($args, $assoc_args){
printf("Welcome to WPCampus%s!!!" . PHP_EOL, ($args[0]) ? ", ${args[0]}" : '');
if(isset($assoc_args['subject'])) {
echo "You are interested in sessions on ${assoc_args['subject']}.", PHP_EOL;
}
});
## Expanded docs for command above, slide 32
/**
* Welcomes a user to WPCampus 2023
*
* ## OPTIONS
* <name>
* : Name of the person to greet
*
* --subject=<subject>
* : What topic sessions interests this person
*
* ## EXAMPLES
*
* # Welcome a user
* $ wp wpcampus Paul --subject=InfoSec
* Welcome to WPCampus, Paul!!!
* You are interested in sessions on InfoSec.
*/
## optional arguments, slide 37
/**
* Welcomes a user to WPCampus 2023
*
* ## OPTIONS
* [<name>]
* : Name of the person to greet
*
* [--subject=<subject>]
* : What topic sessions interests this person
*
* ## EXAMPLES
*
* # Welcome a user
* $ wp wpcampus Paul --subject=InfoSec
* Welcome to WPCampus, Paul!!!
* You are interested in sessions on InfoSec.
*/
## more fun with PHPDocs, slide 40
/**
* Welcomes a user to WPCampus 2023
*
* ## OPTIONS
* [<name>...]
* : Name of the persons to greet
* ---
* default: John
* options:
* - John
* - Paul
* - George
* - Ringo
* ---
*
* [--subject=<subject>]
* : What topic sessions interests this person
*
* ## EXAMPLES
*
* # Welcome a user
* $ wp wpcampus --subject=InfoSec
* Welcome to WPCampus, John!!!
* You are interested in sessions on InfoSec.
*
* # Welcome several users
* $ wp wpcampus Paul Ringo George
* Welcome to WPCampus, Paul, Ringo, George!!!
*/
WP_CLI::add_command('wpcampus',function ($args, $assoc_args){
$msg = "Welcome to WPCampus";
$last = array_key_last($args);
foreach ($args as $index => $name) {
$msg .= ', ';
if ($index === $last && 1 !== count($args)) {
$msg .= 'and ';
}
$msg .= $name;
}
echo $msg,'!!!',PHP_EOL;
if(isset($assoc_args['subject'])) {
echo "You are interested in sessions on ${assoc_args['subject']}", PHP_EOL;
}
});
## Subcommands two functions, slide 45
WP_CLI::add_command('wpcampus greet',function ($args, $assoc_args){
printf("Welcome to WPCampus%s!!!" . PHP_EOL, ($args[0]) ? ", ${args[0]}" : '');
});
WP_CLI::add_command('wpcampus goodbye',function ($args, $assoc_args){
printf("Thanks for being with us%s!!!" . PHP_EOL, ($args[0]) ? ", ${args[0]}" : '');
});
## Subcommands, class with public methods, slide 47
/**
* Greet and say farewell to WPCampus attendees
*/
class wpcampus
{
/**
* Welcomes a user to WPCampus 2023
*
* ## OPTIONS
* <name>
* : Name of the person to greet
*
* --subject=<subject>
* : What topic sessions interests this person
*
* ## EXAMPLES
*
* # Welcome a user
* $ wp wpcampus Paul --subject=InfoSec
* Welcome to WPCampus, Paul!!!
* You are interested in sessions on InfoSec.
*/
public function greet($args, $assoc_args) {
printf("Welcome to WPCampus%s!!!" . PHP_EOL, ($args[0]) ? ", ${args[0]}" : '');
if(isset($assoc_args['subject'])) {
echo "You are interested in sessions on ${assoc_args['subject']}.", PHP_EOL;
}
}
/**
* Say farewell to our participants, and thank our speakers
*
* ## OPTIONS
* [--speaker]
* : was the user a speaker
*
* ## EXAMPLES
*
* # Say farewell
* $ wp wpcampus goodbye --speaker
* Thanks for joining us this year at WPCampus!
* Your contribution as a speaker was truly appreciated!
*
*/
public function goodbye($args, $assoc_args) {
echo "Thanks for joining us this year at WPCampus!", PHP_EOL;
if (isset($assoc_args['speaker']) && $assoc_args['speaker']) {
echo "Your contribution as a speaker was truly appreciated!", PHP_EOL;
}
}
}
WP_CLI::add_command('wpcampus',wpcampus::class);