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