add-category-to-place.php
2 years ago
add-place.php
11 months ago
add-tag-to-place.php
2 years ago
remove-category-from-place.php
2 years ago
remove-tag-from-place.php
2 years ago
remove-category-from-place.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RemoveCategoryFromPlace. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RemoveCategoryFromPlace |
| 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\GeoDirectory\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use SureTriggers\Integrations\GeoDirectory\GeoDirectory; |
| 19 | use Exception; |
| 20 | |
| 21 | /** |
| 22 | * RemoveCategoryFromPlace |
| 23 | * |
| 24 | * @category RemoveCategoryFromPlace |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class RemoveCategoryFromPlace extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'GeoDirectory'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'remove_category_from_place'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Remove Category From Place', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | |
| 62 | return $actions; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Action listener. |
| 67 | * |
| 68 | * @param int $user_id user_id. |
| 69 | * @param int $automation_id automation_id. |
| 70 | * @param array $fields fields. |
| 71 | * @param array $selected_options selectedOptions. |
| 72 | * |
| 73 | * @return array |
| 74 | * @throws Exception Error. |
| 75 | */ |
| 76 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 77 | $post_data = []; |
| 78 | $post_id = $selected_options['post'] ? $selected_options['post'] : 0; |
| 79 | |
| 80 | $post_details = static::gd_update_post( $post_id, $selected_options, false ); |
| 81 | |
| 82 | $post_data = (array) get_post( $post_id ); |
| 83 | return array_merge( $post_data, $post_details ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Update Place Table |
| 88 | * |
| 89 | * @param int $post_id post_id. |
| 90 | * @param array $gd_post post data. |
| 91 | * @param bool $update New or update. |
| 92 | * |
| 93 | * @return array |
| 94 | * @throws Exception Error. |
| 95 | */ |
| 96 | public function gd_update_post( $post_id, $gd_post, $update ) { |
| 97 | global $wpdb; |
| 98 | $tags['categories'] = []; |
| 99 | $post_type = 'gd_place'; |
| 100 | $taxonomy = $post_type . 'category'; |
| 101 | $place_tag = explode( ',', $gd_post['place_categories'] ); |
| 102 | $place_tag_ids = GeoDirectory::get_place_terms( $place_tag, $taxonomy ); |
| 103 | $current_tags = wp_get_post_terms( $post_id, $taxonomy, [ 'fields' => 'ids' ] ); |
| 104 | |
| 105 | if ( is_array( $current_tags ) ) { |
| 106 | $tags_to_remove = array_intersect( $current_tags, $place_tag_ids ); |
| 107 | if ( ! empty( $tags_to_remove ) ) { |
| 108 | wp_remove_object_terms( $post_id, $tags_to_remove, $taxonomy ); |
| 109 | } |
| 110 | } |
| 111 | $placetags = wp_get_post_terms( $post_id, $taxonomy, [ 'fields' => 'names' ] ); |
| 112 | |
| 113 | if ( ! empty( $placetags ) && ! is_wp_error( $placetags ) ) { |
| 114 | $tags['categories'] = $placetags; |
| 115 | } |
| 116 | return $tags; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | } |
| 122 | |
| 123 | RemoveCategoryFromPlace::get_instance(); |
| 124 |