@@ -166,6 +166,90 @@ public function path() {
166166 }
167167 }
168168
169+ /**
170+ * Get variables and constants defined in wp-config.php file.
171+ *
172+ * ## OPTIONS
173+ *
174+ * [--fields=<fields>]
175+ * : Limit the output to specific fields. Defaults to all fields.
176+ *
177+ * [--format=<format>]
178+ * : Render output in a particular format.
179+ * ---
180+ * default: table
181+ * options:
182+ * - table
183+ * - csv
184+ * - json
185+ * - yaml
186+ * ---
187+ *
188+ * ## EXAMPLES
189+ *
190+ * # List variables and constants defined in wp-config.php file.
191+ * $ wp config get --format=table
192+ * +------------------+------------------------------------------------------------------+----------+
193+ * | key | value | type |
194+ * +------------------+------------------------------------------------------------------+----------+
195+ * | table_prefix | wp_ | variable |
196+ * | DB_NAME | wp_cli_test | constant |
197+ * | DB_USER | root | constant |
198+ * | DB_PASSWORD | root | constant |
199+ * | AUTH_KEY | r6+@shP1yO&$)1gdu.hl[/j;7Zrvmt~o;#WxSsa0mlQOi24j2cR,7i+QM/#7S:o^ | constant |
200+ * | SECURE_AUTH_KEY | iO-z!_m--YH$Tx2tf/&V,YW*13Z_HiRLqi)d?$o-tMdY+82pK$`T.NYW~iTLW;xp | constant |
201+ * +------------------+------------------------------------------------------------------+----------+
202+ *
203+ * @when before_wp_load
204+ */
205+ public function get ( $ _ , $ assoc_args ) {
206+ $ default_fields = array (
207+ 'key ' ,
208+ 'value ' ,
209+ 'type ' ,
210+ );
211+
212+ $ defaults = array (
213+ 'fields ' => implode ( ', ' , $ default_fields ),
214+ 'format ' => 'table ' ,
215+ );
216+
217+ $ assoc_args = array_merge ( $ defaults , $ assoc_args );
218+
219+ $ wp_cli_original_defined_constants = get_defined_constants ();
220+ $ wp_cli_original_defined_vars = get_defined_vars ();
221+
222+ eval ( WP_CLI ::get_runner ()->get_wp_config_code () );
223+
224+ $ wp_config_vars = self ::get_wp_config_vars ( get_defined_vars (), $ wp_cli_original_defined_vars , 'variable ' , array ( 'wp_cli_original_defined_vars ' ) );
225+ $ wp_config_constants = self ::get_wp_config_vars ( get_defined_constants (), $ wp_cli_original_defined_constants , 'constant ' );
226+
227+ WP_CLI \Utils \format_items ( $ assoc_args ['format ' ], array_merge ( $ wp_config_vars , $ wp_config_constants ), $ assoc_args ['fields ' ] );
228+ }
229+
230+ /**
231+ * Filter wp-config.php file configurations.
232+ *
233+ * @param array $list
234+ * @param array $previous_list
235+ * @param array $exclude_list
236+ * @return array
237+ */
238+ private static function get_wp_config_vars ( $ list , $ previous_list , $ type , $ exclude_list = array () ) {
239+ $ result = array ();
240+ foreach ( $ list as $ key => $ val ) {
241+ if ( array_key_exists ( $ key , $ previous_list ) || in_array ( $ key , $ exclude_list ) ) {
242+ continue ;
243+ }
244+ $ out = array ();
245+ $ out ['key ' ] = $ key ;
246+ $ out ['value ' ] = $ val ;
247+ $ out ['type ' ] = $ type ;
248+ $ result [] = $ out ;
249+ }
250+ return $ result ;
251+ }
252+
169253 private static function _read ( $ url ) {
170254 $ headers = array ('Accept ' => 'application/json ' );
171255 $ response = Utils \http_request ( 'GET ' , $ url , null , $ headers , array ( 'timeout ' => 30 ) );
0 commit comments