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