PluginProbe
BuddyPress / trunk
BuddyPress vtrunk
11.6.2 12.7.2 14.5.2 11.6.0 12.7.0 2.5.0-rc1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.0-beta1 2.6.0-rc1 2.6.1 2.6.1.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.0-beta1 2.7.0-rc1 2.7.0-rc2 2.7.1 2.7.2 All 274 releases
buddypress / cli / src / email.php

email.php in BuddyPress trunk, at cli/src/email.php

241 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Buddypress\CLI\Command;
4
5 use WP_CLI;
6
7 /**
8 * Manage BuddyPress Emails.
9 *
10 * ## EXAMPLES
11 *
12 * # Create email post
13 * $ wp bp email create --type=new-event --type-description="Send an email when a new event is created" --subject="[{{{site.name}}}] A new event was created" --content="<a href='{{{some.custom-token-url}}}'></a>A new event</a> was created" --plain-text-content="A new event was created"
14 * Success: Email post created for type "new-event".
15 *
16 * # Create email post with content from given file
17 * $ wp bp email create ./email-content.txt --type=new-event --type-description="Send an email when a new event is created" --subject="[{{{site.name}}}] A new event was created" --plain-text-content="A new event was created"
18 * Success: Email post created for type "new-event".
19 *
20 * @since 1.6.0
21 */
22 class Email extends BuddyPressCommand {
23
24 /**
25 * Create a new email post connected to an email type.
26 *
27 * ## OPTIONS
28 *
29 * --type=<type>
30 * : Email type for the email (should be unique identifier, sanitized like a post slug).
31 *
32 * --type-description=<type-description>
33 * : Email type description.
34 *
35 * --subject=<subject>
36 * : Email subject line. Email tokens allowed. View https://codex.buddypress.org/emails/email-tokens/ for more info.
37 *
38 * [--content=<content>]
39 * : Email content. Email tokens allowed. View https://codex.buddypress.org/emails/email-tokens/ for more info.
40 *
41 * [--plain-text-content=<plain-text-content>]
42 * : Plain-text email content. Email tokens allowed. View https://codex.buddypress.org/emails/email-tokens/ for more info.
43 *
44 * [<file>]
45 * : Read content from <file>. If this value is present, the
46 * `--content` argument will be ignored.
47 *
48 * Passing `-` as the filename will cause post content to
49 * be read from STDIN.
50 *
51 * [--edit]
52 * : Immediately open system's editor to write or edit email content.
53 *
54 * If content is read from a file, from STDIN, or from the `--content`
55 * argument, that text will be loaded into the editor.
56 *
57 * ## EXAMPLES
58 *
59 * # Create email post
60 * $ wp bp email create --type=new-event --type-description="Send an email when a new event is created" --subject="[{{{site.name}}}] A new event was created" --content="<a href='{{{some.custom-token-url}}}'></a>A new event</a> was created" --plain-text-content="A new event was created"
61 * Success: Email post created for type "new-event".
62 *
63 * # Create email post with content from given file
64 * $ wp bp email create ./email-content.txt --type=new-event --type-description="Send an email when a new event is created" --subject="[{{{site.name}}}] A new event was created" --plain-text-content="A new event was created"
65 * Success: Email post created for type "new-event".
66 *
67 * @alias add
68 */
69 public function create( $args, $assoc_args ) {
70 $switched = false;
71
72 if ( false === bp_is_root_blog() ) {
73 $switched = true;
74 switch_to_blog( bp_get_root_blog_id() );
75 }
76
77 $term = term_exists( $assoc_args['type'], bp_get_email_tax_type() );
78
79 // Term already exists, so don't do anything.
80 if ( 0 !== $term && null !== $term ) {
81 if ( true === $switched ) {
82 restore_current_blog();
83 }
84
85 WP_CLI::error( sprintf( 'Email type %s already exists.', $assoc_args['type'] ) );
86 }
87
88 if ( ! empty( $args[0] ) && \method_exists( $this, 'read_from_file_or_stdin' ) ) {
89 $assoc_args['content'] = $this->read_from_file_or_stdin( $args[0] );
90 }
91
92 if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'edit' ) ) {
93 $input = \WP_CLI\Utils\get_flag_value( $assoc_args, 'content', '' );
94 $output = $this->edit_email( $input, 'WP-CLI: New BP Email Content' );
95
96 $assoc_args['content'] = ( $output ) ? $output : $input;
97 }
98
99 $defaults = [
100 'post_status' => 'publish',
101 'post_type' => bp_get_email_post_type(),
102 ];
103
104 $email = [
105 'post_title' => $assoc_args['subject'],
106 'post_content' => $assoc_args['content'],
107 'post_excerpt' => ! empty( $assoc_args['plain-text-content'] ) ? $assoc_args['plain-text-content'] : '',
108 ];
109
110 $id = $assoc_args['type'];
111
112 // Email post content.
113 $post_id = wp_insert_post( bp_parse_args( $email, $defaults, 'install_email_' . $id ), true );
114
115 // Save the situation.
116 if ( ! is_wp_error( $post_id ) ) {
117 $tt_ids = wp_set_object_terms( $post_id, $id, bp_get_email_tax_type() );
118
119 // Situation description.
120 if ( ! is_wp_error( $tt_ids ) && ! empty( $assoc_args['type-description'] ) ) {
121 $term = get_term_by( 'term_taxonomy_id', (int) $tt_ids[0], bp_get_email_tax_type() );
122
123 wp_update_term(
124 (int) $term->term_id,
125 bp_get_email_tax_type(),
126 [
127 'description' => $assoc_args['type-description'],
128 ]
129 );
130 }
131
132 if ( true === $switched ) {
133 restore_current_blog();
134 }
135
136 WP_CLI::success( sprintf( 'Email post created for type %s.', $assoc_args['type'] ) );
137 } else {
138 if ( true === $switched ) {
139 restore_current_blog();
140 }
141
142 WP_CLI::error( "There was a problem creating the email post for type '{$assoc_args['type']}' - " . $post_id->get_error_message() );
143 }
144 }
145
146 /**
147 * Get details for a post connected to an email type.
148 *
149 * ## OPTIONS
150 *
151 * <type>
152 * : The email type to fetch the post details for.
153 *
154 * [--field=<field>]
155 * : Instead of returning the whole post, returns the value of a single field.
156 *
157 * [--fields=<fields>]
158 * : Limit the output to specific fields.
159 *
160 * [--format=<format>]
161 * : Render output in a particular format.
162 * ---
163 * default: table
164 * options:
165 * - table
166 * - csv
167 * - json
168 * - yaml
169 * ---
170 *
171 * ## EXAMPLE
172 *
173 * # Output the post ID for the 'activity-at-message' email type
174 * $ wp bp email get-post activity-at-message --fields=ID
175 *
176 * @alias get-post
177 * @alias see
178 */
179 public function get_post( $args, $assoc_args ) {
180 $email = bp_get_email( $args[0] );
181
182 if ( is_wp_error( $email ) ) {
183 WP_CLI::error( sprintf( 'Email post for type %s does not exist.', $args[0] ) );
184 }
185
186 $post_arr = get_object_vars( $email->get_post_object() );
187 unset( $post_arr['filter'] );
188 if ( empty( $assoc_args['fields'] ) ) {
189 $assoc_args['fields'] = array_keys( $post_arr );
190 }
191
192 $this->get_formatter( $assoc_args )->display_item( $email->get_post_object() );
193 }
194
195 /**
196 * Reinstall BuddyPress default emails.
197 *
198 * ## OPTIONS
199 *
200 * [--yes]
201 * : Answer yes to the confirmation message.
202 *
203 * ## EXAMPLE
204 *
205 * # Reinstall BuddyPress default emails.
206 * $ wp bp email reinstall --yes
207 * Success: Emails have been successfully reinstalled.
208 */
209 public function reinstall( $args, $assoc_args ) {
210 WP_CLI::confirm( 'Are you sure you want to reinstall BuddyPress emails?', $assoc_args );
211
212 require_once buddypress()->plugin_dir . 'bp-core/admin/bp-core-admin-tools.php';
213
214 $result = bp_admin_reinstall_emails();
215
216 if ( 0 === $result[0] ) {
217 WP_CLI::success( $result[1] );
218 } else {
219 WP_CLI::error( $result[1] );
220 }
221 }
222
223 /**
224 * Helper method to use the '--edit' flag.
225 *
226 * Copied from Post_Command::_edit().
227 *
228 * @param string $content Post content.
229 * @param string $title Post title.
230 * @return mixed
231 */
232 protected function edit_email( $content, $title ) {
233 $content = apply_filters( 'the_editor_content', $content );
234 $output = \WP_CLI\Utils\launch_editor_for_input( $content, $title );
235
236 return ( is_string( $output ) ) ?
237 apply_filters( 'content_save_pre', $output )
238 : $output;
239 }
240 }
241