Reset.php
285 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\Tools; |
| 4 | |
| 5 | use PodsForm; |
| 6 | use Pods\Whatsit\Field; |
| 7 | use Pods\Whatsit\Pod; |
| 8 | |
| 9 | /** |
| 10 | * Reset tool functionality. |
| 11 | * |
| 12 | * @since 2.9.10 |
| 13 | */ |
| 14 | class Reset extends Base { |
| 15 | |
| 16 | /** |
| 17 | * Delete all content for a Pod. |
| 18 | * |
| 19 | * @since 2.9.10 |
| 20 | * |
| 21 | * @param Pod $pod The Pod object. |
| 22 | * @param string $mode The reset mode (preview or full). |
| 23 | * |
| 24 | * @return array The results with information about the reset done. |
| 25 | */ |
| 26 | public function delete_all_content_for_pod( Pod $pod, $mode ) { |
| 27 | $this->setup(); |
| 28 | |
| 29 | $this->errors = []; |
| 30 | |
| 31 | $results = []; |
| 32 | |
| 33 | if ( 'preview' !== $mode ) { |
| 34 | $this->api->reset_pod( [], $pod ); |
| 35 | } |
| 36 | |
| 37 | $results[ __( 'Delete all content for pod' ) ] = __( 'Pod content has been deleted' ); |
| 38 | |
| 39 | $tool_heading = sprintf( |
| 40 | // translators: %s: The Pod label. |
| 41 | __( 'Reset results for %s', 'pods' ), |
| 42 | $pod->get_label() . ' (' . $pod->get_name() . ')' |
| 43 | ); |
| 44 | |
| 45 | $results['message_html'] = $this->get_message_html( $tool_heading, $results, $mode ); |
| 46 | |
| 47 | return $results; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Delete all relationship data for a Pod. |
| 52 | * |
| 53 | * @since 2.9.10 |
| 54 | * |
| 55 | * @param Pod $pod The Pod object. |
| 56 | * @param null|string|array $field_names The fields to use (comma-separated or an array), otherwise delete relationships for all fields on Pod. |
| 57 | * @param string $mode The reset mode (preview or full). |
| 58 | * |
| 59 | * @return array The results with information about the reset done. |
| 60 | */ |
| 61 | public function delete_all_relationship_data_for_pod( Pod $pod, $field_names, $mode ) { |
| 62 | $this->setup(); |
| 63 | |
| 64 | $this->errors = []; |
| 65 | |
| 66 | if ( $field_names ) { |
| 67 | $fields = []; |
| 68 | |
| 69 | if ( is_string( $field_names ) ) { |
| 70 | $field_names = explode( ',', $field_names ); |
| 71 | $field_names = pods_trim( $field_names ); |
| 72 | $field_names = array_filter( $field_names ); |
| 73 | |
| 74 | foreach ( $field_names as $field_name ) { |
| 75 | $field = $pod->get_field( $field_name ); |
| 76 | |
| 77 | if ( $field ) { |
| 78 | $fields[] = $field; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } else { |
| 83 | $fields = $pod->get_fields( [ |
| 84 | 'type' => PodsForm::tableless_field_types(), |
| 85 | ] ); |
| 86 | } |
| 87 | |
| 88 | $results = []; |
| 89 | |
| 90 | global $wpdb; |
| 91 | |
| 92 | foreach ( $fields as $field_to_delete_from_podsrel ) { |
| 93 | if ( 'preview' !== $mode ) { |
| 94 | $total_deleted_from_podsrel = (int) $wpdb->query( |
| 95 | $wpdb->prepare( |
| 96 | " |
| 97 | DELETE * |
| 98 | FROM `{$wpdb->prefix}podsrel` |
| 99 | WHERE `pod_id` = %d AND `field_id` = %d |
| 100 | ", |
| 101 | [ |
| 102 | $pod->get_id(), |
| 103 | $field_to_delete_from_podsrel->get_id(), |
| 104 | ] |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | $total_related_deleted_from_podsrel = (int) $wpdb->query( |
| 109 | $wpdb->prepare( |
| 110 | " |
| 111 | DELETE * |
| 112 | FROM `{$wpdb->prefix}podsrel` |
| 113 | WHERE `related_pod_id` = %d AND `related_field_id` = %d |
| 114 | ", |
| 115 | [ |
| 116 | $pod->get_id(), |
| 117 | $field_to_delete_from_podsrel->get_id(), |
| 118 | ] |
| 119 | ) |
| 120 | ); |
| 121 | } else { |
| 122 | $total_deleted_from_podsrel = (int) $wpdb->get_var( |
| 123 | $wpdb->prepare( |
| 124 | " |
| 125 | SELECT COUNT(*) |
| 126 | FROM `{$wpdb->prefix}podsrel` |
| 127 | WHERE `pod_id` = %d AND `field_id` = %d |
| 128 | ", |
| 129 | [ |
| 130 | $pod->get_id(), |
| 131 | $field_to_delete_from_podsrel->get_id(), |
| 132 | ] |
| 133 | ) |
| 134 | ); |
| 135 | |
| 136 | $total_related_deleted_from_podsrel = (int) $wpdb->get_var( |
| 137 | $wpdb->prepare( |
| 138 | " |
| 139 | SELECT COUNT(*) |
| 140 | FROM `{$wpdb->prefix}podsrel` |
| 141 | WHERE `related_pod_id` = %d AND `related_field_id` = %d |
| 142 | ", |
| 143 | [ |
| 144 | $pod->get_id(), |
| 145 | $field_to_delete_from_podsrel->get_id(), |
| 146 | ] |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | $heading = sprintf( |
| 152 | // translators: %1$s: The field label; %2$s: The field name. |
| 153 | __( 'Relationship data removed from podsrel table for field: %1$s (%2$s)', 'pods' ), |
| 154 | $field_to_delete_from_podsrel->get_label(), |
| 155 | $field_to_delete_from_podsrel->get_name() |
| 156 | ); |
| 157 | |
| 158 | $results[ $heading ] = sprintf( |
| 159 | '%1$s %2$s', |
| 160 | number_format_i18n( $total_deleted_from_podsrel ), |
| 161 | _n( 'row', 'rows', $total_deleted_from_podsrel, 'pods' ) |
| 162 | ); |
| 163 | |
| 164 | $heading = sprintf( |
| 165 | // translators: %1$s: The field label; %2$s: The field name. |
| 166 | __( 'Bidirectional Relationship data removed from podsrel table for field: %1$s (%2$s)', 'pods' ), |
| 167 | $field_to_delete_from_podsrel->get_label(), |
| 168 | $field_to_delete_from_podsrel->get_name() |
| 169 | ); |
| 170 | |
| 171 | $results[ $heading ] = sprintf( |
| 172 | '%1$s %2$s', |
| 173 | number_format_i18n( $total_related_deleted_from_podsrel ), |
| 174 | _n( 'row', 'rows', $total_related_deleted_from_podsrel, 'pods' ) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | $tool_heading = sprintf( |
| 179 | // translators: %s: The Pod label. |
| 180 | __( 'Reset results for %s', 'pods' ), |
| 181 | $pod->get_label() . ' (' . $pod->get_name() . ')' |
| 182 | ); |
| 183 | |
| 184 | $results['message_html'] = $this->get_message_html( $tool_heading, $results, $mode ); |
| 185 | |
| 186 | return $results; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Delete all Groups and Fields for a Pod. |
| 191 | * |
| 192 | * @since 2.9.10 |
| 193 | * |
| 194 | * @param Pod $pod The Pod object. |
| 195 | * @param string $mode The reset mode (preview or full). |
| 196 | * |
| 197 | * @return array The results with information about the reset done. |
| 198 | */ |
| 199 | public function delete_all_groups_and_fields_for_pod( Pod $pod, $mode ) { |
| 200 | $this->setup(); |
| 201 | |
| 202 | $this->errors = []; |
| 203 | |
| 204 | $results = []; |
| 205 | |
| 206 | if ( 'preview' !== $mode ) { |
| 207 | $results[ __( 'Delete all fields for pod', 'pods' ) ] = $this->delete_all_fields_for_pod( $pod, $mode ); |
| 208 | $results[ __( 'Delete all groups for pod', 'pods' ) ] = $this->delete_all_groups_for_pod( $pod, $mode ); |
| 209 | } |
| 210 | |
| 211 | $tool_heading = sprintf( |
| 212 | // translators: %s: The Pod label. |
| 213 | __( 'Reset results for %s', 'pods' ), |
| 214 | $pod->get_label() . ' (' . $pod->get_name() . ')' |
| 215 | ); |
| 216 | |
| 217 | $results['message_html'] = $this->get_message_html( $tool_heading, $results, $mode ); |
| 218 | |
| 219 | return $results; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Delete all Groups and Fields for a Pod. |
| 224 | * |
| 225 | * @since 2.9.10 |
| 226 | * |
| 227 | * @param Pod $pod The Pod object. |
| 228 | * @param string $mode The reset mode (preview or full). |
| 229 | * |
| 230 | * @return string The text result. |
| 231 | */ |
| 232 | protected function delete_all_fields_for_pod( Pod $pod, $mode ) { |
| 233 | $this->setup(); |
| 234 | |
| 235 | $fields = $pod->get_fields(); |
| 236 | |
| 237 | $total_deleted = count( $fields ); |
| 238 | |
| 239 | foreach ( $fields as $field ) { |
| 240 | if ( 'preview' !== $mode ) { |
| 241 | $this->api->delete_field( $field ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // translators: %1$s: The total number of fields deleted; %2$s: The singular or plural text for fields. |
| 246 | return sprintf( |
| 247 | _x( 'Deleted %1$s %2$s', 'The text for how many fields were deleted', 'pods' ), |
| 248 | number_format_i18n( $total_deleted ), |
| 249 | _n( 'field', 'fields', $total_deleted, 'pods' ) |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Delete all Groups and Fields for a Pod. |
| 255 | * |
| 256 | * @since 2.9.10 |
| 257 | * |
| 258 | * @param Pod $pod The Pod object. |
| 259 | * @param string $mode The reset mode (preview or full). |
| 260 | * |
| 261 | * @return string The text result. |
| 262 | */ |
| 263 | protected function delete_all_groups_for_pod( Pod $pod, $mode ) { |
| 264 | $this->setup(); |
| 265 | |
| 266 | $groups = $pod->get_groups(); |
| 267 | |
| 268 | $total_deleted = count( $groups ); |
| 269 | |
| 270 | foreach ( $groups as $group ) { |
| 271 | if ( 'preview' !== $mode ) { |
| 272 | $this->api->delete_group( $group ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // translators: %1$s: The total number of groups deleted; %2$s: The singular or plural text for groups. |
| 277 | return sprintf( |
| 278 | _x( 'Deleted %1$s %2$s', 'The text for how many groups were deleted', 'pods' ), |
| 279 | number_format_i18n( $total_deleted ), |
| 280 | _n( 'group', 'groups', $total_deleted, 'pods' ) |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | } |
| 285 |