add-new-media.php
9 months ago
add-new-role.php
9 months ago
add-tag-to-post.php
9 months ago
add-taxonomy-to-post.php
9 months ago
change-role.php
9 months ago
create-category.php
9 months ago
create-comment.php
9 months ago
create-post.php
8 months ago
create-role.php
9 months ago
create-tag.php
9 months ago
create-user-if-not-exists.php
1 year ago
delete-user.php
9 months ago
find-posts.php
3 months ago
find-user-by-email.php
2 years ago
find-user-by-id.php
2 years ago
find-user-meta-by-key.php
2 years ago
find-user-metas.php
2 years ago
get-post-by-id.php
9 months ago
get-post-metadata.php
9 months ago
get-post-taxonomy.php
9 months ago
get-post-terms.php
9 months ago
get-taxonomy-by-name.php
9 months ago
get-user-by-role.php
9 months ago
remove-role.php
9 months ago
remove-user-meta.php
9 months ago
remove-user.php
9 months ago
send-mail.php
3 months ago
set-post-meta.php
9 months ago
set-user-meta.php
9 months ago
update-comment-status.php
9 months ago
update-post-excerpt.php
9 months ago
update-post.php
9 months ago
update-user.php
3 months ago
create-post.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreatePost. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreatePost |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\Wordpress\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use Exception; |
| 19 | |
| 20 | /** |
| 21 | * CreatePost |
| 22 | * |
| 23 | * @category CreatePost |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class CreatePost extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'WordPress'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'create_update_post'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'Post: Create a Post', 'suretriggers' ), |
| 57 | 'action' => 'create_update_post', |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 60 | |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * |
| 72 | * @return array|bool|object |
| 73 | * @throws Exception Error. |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | $result_arr = []; |
| 77 | $is_post_update = false; |
| 78 | foreach ( $fields as $field ) { |
| 79 | if ( isset( $field['name'] ) && isset( $selected_options[ $field['name'] ] ) && ( trim( wp_strip_all_tags( $selected_options[ $field['name'] ] ) ) !== '' ) ) { |
| 80 | if ( 'post_content' === $field['name'] ) { |
| 81 | $html_content = $selected_options[ $field['name'] ]; |
| 82 | $patterns = [ |
| 83 | '/<head\b[^>]*>.*?<\/head>/is', |
| 84 | '/<script\b[^>]*>.*?<\/script>/is', |
| 85 | '/<style\b[^>]*>.*?<\/style>/is', |
| 86 | ]; |
| 87 | $html_content = preg_replace( $patterns, '', $html_content ); |
| 88 | $result_arr[ $field['name'] ] = $html_content; |
| 89 | } elseif ( 'post_date' === $field['name'] ) { |
| 90 | // Handle post_date field for scheduling. |
| 91 | $result_arr[ $field['name'] ] = $selected_options[ $field['name'] ]; |
| 92 | } else { |
| 93 | $result_arr[ $field['name'] ] = $selected_options[ $field['name'] ]; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $meta_array = []; |
| 99 | |
| 100 | if ( ! empty( $selected_options['post_meta'] ) ) { |
| 101 | foreach ( $selected_options['post_meta'] as $meta ) { |
| 102 | $meta_key = $meta['metaKey']; |
| 103 | $meta_value = $meta['metaValue']; |
| 104 | $meta_array[ $meta_key ] = $meta_value; |
| 105 | } |
| 106 | $result_arr['meta_input'] = $meta_array; |
| 107 | } |
| 108 | |
| 109 | if ( ! empty( $selected_options['post_url'] ) ) { |
| 110 | $url = $selected_options['post_url']; |
| 111 | $parts = explode( '/', $url ); |
| 112 | $parts = array_values( array_filter( $parts ) ); |
| 113 | $slug = $parts[ count( $parts ) - 1 ]; |
| 114 | |
| 115 | $post_exists = get_page_by_path( $slug, OBJECT, $selected_options['post_type'] ); |
| 116 | |
| 117 | if ( $post_exists ) { |
| 118 | $result_arr['ID'] = $post_exists->ID; |
| 119 | wp_update_post( $result_arr ); |
| 120 | $last_response = get_post( $post_exists->ID ); |
| 121 | $post_id = $post_exists->ID; |
| 122 | $is_post_update = true; |
| 123 | } else { |
| 124 | return [ |
| 125 | 'status' => 'error', |
| 126 | 'message' => 'The URL entered is incorrect. Please provide the correct URL for the post.', |
| 127 | ]; |
| 128 | } |
| 129 | } elseif ( ! empty( $selected_options['post_id'] ) ) { |
| 130 | $post_id = absint( $selected_options['post_id'] ); |
| 131 | $post_exists = get_post( $post_id ); |
| 132 | |
| 133 | if ( $post_exists && $post_exists instanceof \WP_Post ) { |
| 134 | $result_arr['ID'] = $post_id; |
| 135 | wp_update_post( $result_arr ); |
| 136 | $last_response = get_post( $post_id ); |
| 137 | $is_post_update = true; |
| 138 | } else { |
| 139 | return [ |
| 140 | 'status' => 'error', |
| 141 | 'message' => 'Invalid Post ID provided. No post found with that ID.', |
| 142 | ]; |
| 143 | } |
| 144 | } else { |
| 145 | /** |
| 146 | * Post ID. |
| 147 | * |
| 148 | * @var int|\WP_Error $post_id |
| 149 | */ |
| 150 | $post_id = wp_insert_post( $result_arr ); |
| 151 | if ( is_wp_error( $post_id ) || 0 === $post_id ) { |
| 152 | $this->set_error( |
| 153 | [ |
| 154 | 'post_data' => $result_arr, |
| 155 | 'msg' => __( 'Failed to insert post!', 'suretriggers' ), |
| 156 | ] |
| 157 | ); |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Handle scheduled posts. |
| 163 | if ( isset( $selected_options['post_status'] ) && 'future' === $selected_options['post_status'] ) { |
| 164 | if ( ! empty( $selected_options['post_date'] ) ) { |
| 165 | // Validate date is in future. |
| 166 | $schedule_date = strtotime( $selected_options['post_date'] ); |
| 167 | $current_time = time(); |
| 168 | |
| 169 | if ( $schedule_date > $current_time ) { |
| 170 | // Update post with scheduled date. |
| 171 | wp_update_post( |
| 172 | [ |
| 173 | 'ID' => $post_id, |
| 174 | 'post_date' => $selected_options['post_date'], |
| 175 | 'post_status' => 'future', |
| 176 | ] |
| 177 | ); |
| 178 | // Post scheduled successfully. |
| 179 | } else { |
| 180 | // Date is in past, publish immediately. |
| 181 | wp_update_post( |
| 182 | [ |
| 183 | 'ID' => $post_id, |
| 184 | 'post_status' => 'publish', |
| 185 | ] |
| 186 | ); |
| 187 | // Schedule date in past, post published immediately. |
| 188 | } |
| 189 | } else { |
| 190 | // If no date provided, change status to draft. |
| 191 | wp_update_post( |
| 192 | [ |
| 193 | 'ID' => $post_id, |
| 194 | 'post_status' => 'draft', |
| 195 | ] |
| 196 | ); |
| 197 | // No schedule date provided, post saved as draft. |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | $last_response = get_post( $post_id ); |
| 202 | $response_taxonomy = ''; |
| 203 | $taxonomy_terms = []; |
| 204 | |
| 205 | // Set taxonomy terms for new post. |
| 206 | if ( isset( $selected_options['taxonomy'] ) && isset( $selected_options['taxonomy_term'] ) ) { |
| 207 | |
| 208 | $terms = []; |
| 209 | $taxonomy = $selected_options['taxonomy']; |
| 210 | $taxonomy_terms_input = $selected_options['taxonomy_term']; |
| 211 | |
| 212 | // Handle different input formats. |
| 213 | if ( is_string( $taxonomy_terms_input ) ) { |
| 214 | $taxonomy_terms_input = array_map( 'trim', explode( ',', $taxonomy_terms_input ) ); |
| 215 | } elseif ( ! is_array( $taxonomy_terms_input ) ) { |
| 216 | // Convert any other format to array. |
| 217 | $taxonomy_terms_input = [ $taxonomy_terms_input ]; |
| 218 | } |
| 219 | |
| 220 | foreach ( $taxonomy_terms_input as $term ) { |
| 221 | if ( is_array( $term ) && isset( $term['value'] ) ) { |
| 222 | // If term value is numeric, it's an existing term ID. |
| 223 | if ( is_numeric( $term['value'] ) ) { |
| 224 | $terms[] = (int) $term['value']; |
| 225 | } else { |
| 226 | // If term value is text, check if term exists or create new one. |
| 227 | $term_name = sanitize_text_field( $term['value'] ); |
| 228 | $existing_term = get_term_by( 'name', $term_name, $taxonomy ); |
| 229 | |
| 230 | if ( $existing_term ) { |
| 231 | // Term exists, use its ID. |
| 232 | $terms[] = (int) $existing_term->term_id; |
| 233 | } else { |
| 234 | // Term doesn't exist, create new term. |
| 235 | $new_term = wp_insert_term( $term_name, $taxonomy ); |
| 236 | if ( ! is_wp_error( $new_term ) ) { |
| 237 | $terms[] = (int) $new_term['term_id']; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } elseif ( is_string( $term ) || is_numeric( $term ) ) { |
| 242 | // Handle direct string/numeric values. |
| 243 | if ( is_numeric( $term ) ) { |
| 244 | $terms[] = (int) $term; |
| 245 | } else { |
| 246 | $term_name = sanitize_text_field( $term ); |
| 247 | $existing_term = get_term_by( 'name', $term_name, $taxonomy ); |
| 248 | |
| 249 | if ( $existing_term ) { |
| 250 | $terms[] = (int) $existing_term->term_id; |
| 251 | } else { |
| 252 | $new_term = wp_insert_term( $term_name, $taxonomy ); |
| 253 | if ( ! is_wp_error( $new_term ) ) { |
| 254 | $terms[] = (int) $new_term['term_id']; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if ( ! empty( $terms ) ) { |
| 262 | if ( $is_post_update ) { |
| 263 | // If is post update then append the terms to the existing terms. |
| 264 | wp_set_object_terms( $post_id, $terms, $taxonomy, true ); |
| 265 | } else { |
| 266 | // If is post create then set the terms. |
| 267 | wp_set_object_terms( $post_id, $terms, $taxonomy, false ); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | $response_taxonomy = get_object_taxonomies( (string) get_post_type( $post_id ) ); |
| 272 | foreach ( $response_taxonomy as $taxonomy_name ) { |
| 273 | $terms = wp_get_post_terms( $post_id, $taxonomy_name ); |
| 274 | if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 275 | foreach ( $terms as $term ) { |
| 276 | $taxonomy_terms[] = $term; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Handle multiple taxonomy terms mapping - enhanced map button functionality. |
| 283 | if ( isset( $selected_options['taxonomy_terms_map'] ) && is_array( $selected_options['taxonomy_terms_map'] ) ) { |
| 284 | foreach ( $selected_options['taxonomy_terms_map'] as $taxonomy_mapping ) { |
| 285 | if ( isset( $taxonomy_mapping['taxonomy'] ) && isset( $taxonomy_mapping['terms'] ) ) { |
| 286 | $taxonomy_name = $taxonomy_mapping['taxonomy']; |
| 287 | $mapped_terms = []; |
| 288 | |
| 289 | // Process mapped terms - support creating new terms if they don't exist. |
| 290 | if ( is_array( $taxonomy_mapping['terms'] ) ) { |
| 291 | foreach ( $taxonomy_mapping['terms'] as $term_data ) { |
| 292 | if ( is_array( $term_data ) && isset( $term_data['value'] ) ) { |
| 293 | $term_value = $term_data['value']; |
| 294 | } elseif ( is_string( $term_data ) || is_numeric( $term_data ) ) { |
| 295 | $term_value = $term_data; |
| 296 | } else { |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if ( is_numeric( $term_value ) ) { |
| 301 | // Existing term ID. |
| 302 | $mapped_terms[] = (int) $term_value; |
| 303 | } else { |
| 304 | // Term name - check if exists or create new. |
| 305 | $term_name = sanitize_text_field( $term_value ); |
| 306 | $existing_term = get_term_by( 'name', $term_name, $taxonomy_name ); |
| 307 | |
| 308 | if ( $existing_term ) { |
| 309 | $mapped_terms[] = (int) $existing_term->term_id; |
| 310 | } else { |
| 311 | // Create new term. |
| 312 | $new_term = wp_insert_term( $term_name, $taxonomy_name ); |
| 313 | if ( ! is_wp_error( $new_term ) ) { |
| 314 | $mapped_terms[] = (int) $new_term['term_id']; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if ( ! empty( $mapped_terms ) ) { |
| 322 | // Set or append terms based on configuration. |
| 323 | $append_terms = isset( $taxonomy_mapping['append'] ) ? $taxonomy_mapping['append'] : false; |
| 324 | if ( $is_post_update && $append_terms ) { |
| 325 | wp_set_object_terms( $post_id, $mapped_terms, $taxonomy_name, true ); |
| 326 | } else { |
| 327 | wp_set_object_terms( $post_id, $mapped_terms, $taxonomy_name, false ); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Update taxonomy terms response for mapped taxonomies. |
| 334 | $response_taxonomy = get_object_taxonomies( (string) get_post_type( $post_id ) ); |
| 335 | foreach ( $response_taxonomy as $taxonomy_name ) { |
| 336 | $terms = wp_get_post_terms( $post_id, $taxonomy_name ); |
| 337 | if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 338 | foreach ( $terms as $term ) { |
| 339 | $taxonomy_terms[] = $term; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if ( ! empty( $selected_options['featured_image'] ) ) { |
| 346 | $image_url = $selected_options['featured_image']; |
| 347 | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 348 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 349 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 350 | |
| 351 | $existing_media_id = absint( attachment_url_to_postid( $image_url ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid |
| 352 | |
| 353 | if ( 0 !== $existing_media_id ) { |
| 354 | $attachment_id = $existing_media_id; |
| 355 | } else { |
| 356 | $attachment_id = media_sideload_image( $image_url, $post_id, null, 'id' ); |
| 357 | } |
| 358 | if ( isset( $selected_options['featured_image'] ) && ! $attachment_id || is_wp_error( $attachment_id ) ) { |
| 359 | |
| 360 | return (object) [ |
| 361 | $last_response, |
| 362 | 'taxonomy_term' => $taxonomy_terms, |
| 363 | 'featured_image_url' => 'Failed to set featured image', |
| 364 | ]; |
| 365 | } |
| 366 | |
| 367 | set_post_thumbnail( $post_id, (int) $attachment_id ); |
| 368 | } |
| 369 | $featured_image_url = get_the_post_thumbnail_url( $post_id, 'full' ); |
| 370 | |
| 371 | return (object) [ |
| 372 | $last_response, |
| 373 | 'taxonomy_term' => $taxonomy_terms, |
| 374 | 'featured_image_url' => $featured_image_url, |
| 375 | ]; |
| 376 | |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | CreatePost::get_instance(); |
| 381 |