Base.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\Tools; |
| 4 | |
| 5 | use PodsAPI; |
| 6 | use WP_CLI; |
| 7 | |
| 8 | /** |
| 9 | * Base tool functionality. |
| 10 | * |
| 11 | * @since 2.9.10 |
| 12 | */ |
| 13 | class Base { |
| 14 | |
| 15 | /** |
| 16 | * @var PodsAPI |
| 17 | */ |
| 18 | protected $api; |
| 19 | |
| 20 | /** |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $errors = []; |
| 24 | |
| 25 | /** |
| 26 | * Setup the tool. |
| 27 | * |
| 28 | * @since 2.9.10 |
| 29 | */ |
| 30 | protected function setup() { |
| 31 | if ( ! $this->api ) { |
| 32 | $this->api = pods_api(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the message HTML from the results. |
| 38 | * |
| 39 | * @since 2.9.10 |
| 40 | * |
| 41 | * @param string $tool_heading The tool heading text. |
| 42 | * @param array $results The tool results. |
| 43 | * @param null|string $mode The tool mode. |
| 44 | * |
| 45 | * @return string The message HTML. |
| 46 | */ |
| 47 | protected function get_message_html( $tool_heading, array $results, $mode = null ) { |
| 48 | $using_cli = defined( 'WP_CLI' ); |
| 49 | |
| 50 | $messages = []; |
| 51 | |
| 52 | if ( $tool_heading ) { |
| 53 | if ( $using_cli ) { |
| 54 | WP_CLI::line( '=== ' . $tool_heading . ' ===' ); |
| 55 | } else { |
| 56 | $messages[] = sprintf( |
| 57 | '<h3>%s</h3>', |
| 58 | $tool_heading |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if ( 'preview' === $mode ) { |
| 64 | $results = array_merge( |
| 65 | [ |
| 66 | __( 'Preview Mode Active', 'pods' ) => __( 'These results did not add or change anything in the database.', 'pods' ), |
| 67 | ], |
| 68 | $results |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | $has_errors = ! empty( $this->errors ); |
| 73 | |
| 74 | $errors_heading = __( 'Errors', 'pods' ); |
| 75 | |
| 76 | if ( $has_errors ) { |
| 77 | $results[ $errors_heading ] = $this->errors; |
| 78 | } |
| 79 | |
| 80 | foreach ( $results as $heading => $result_set ) { |
| 81 | if ( ! is_array( $result_set ) ) { |
| 82 | $result_set = (array) $result_set; |
| 83 | } |
| 84 | |
| 85 | if ( empty( $result_set ) ) { |
| 86 | // Don't output anything if in upgrade mode. |
| 87 | if ( 'upgrade' === $mode ) { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | $result_set[] = __( 'No actions were needed.', 'pods' ); |
| 92 | } |
| 93 | |
| 94 | if ( $using_cli ) { |
| 95 | if ( $errors_heading === $heading ) { |
| 96 | WP_CLI::warning( $heading . '...' ); |
| 97 | |
| 98 | foreach ( $result_set as $result ) { |
| 99 | WP_CLI::warning( '- ' . $result ); |
| 100 | } |
| 101 | } else { |
| 102 | WP_CLI::line( $heading . '...' ); |
| 103 | |
| 104 | foreach ( $result_set as $result ) { |
| 105 | WP_CLI::line( '- ' . $result ); |
| 106 | } |
| 107 | } |
| 108 | } else { |
| 109 | $messages[] = sprintf( |
| 110 | '<h4>%1$s</h4><ul class="ul-disc"><li>%2$s</li></ul>', |
| 111 | esc_html( $heading ), |
| 112 | implode( '</li><li>', array_map( 'esc_html', $result_set ) ) |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $total_messages = count( $messages ); |
| 118 | |
| 119 | $total_check = $tool_heading ? 1 : 0; |
| 120 | |
| 121 | if ( $total_messages <= $total_check ) { |
| 122 | if ( $using_cli ) { |
| 123 | WP_CLI::warning( __( 'No actions were needed.', 'pods' ) ); |
| 124 | } else { |
| 125 | // Don't output anything if in upgrade mode. |
| 126 | if ( 'upgrade' === $mode ) { |
| 127 | return ''; |
| 128 | } |
| 129 | |
| 130 | $messages[] = esc_html__( 'No actions were needed.', 'pods' ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if ( $using_cli ) { |
| 135 | if ( $has_errors ) { |
| 136 | WP_CLI::error( __( 'This tool was unable to complete', 'pods' ) ); |
| 137 | } |
| 138 | |
| 139 | return ''; |
| 140 | } |
| 141 | |
| 142 | return wpautop( implode( "\n\n", $messages ) ); |
| 143 | } |
| 144 | |
| 145 | } |
| 146 |