| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Implements Pods command for WP-CLI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class Pods_CLI_Command extends WP_CLI_Command { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 * @synopsis --pod=<pod> --<field>=<value> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 12 |  | View Code Duplication | 	function add( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		$pod  = $assoc_args['pod']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		$item = pods_var_raw( 'item', $assoc_args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		unset( $assoc_args['pod'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		if ( isset( $assoc_args['item'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			unset( $assoc_args['item'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		if ( ! empty( $assoc_args ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			$id = pods( $pod, $item )->save( $assoc_args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			if ( 0 < $id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 				WP_CLI::success( __( 'Pod item added', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 				WP_CLI::line( "ID: {$id}" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 				WP_CLI::error( __( 'Error saving pod item', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			WP_CLI::error( __( 'No data sent for saving', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @synopsis --pod=<pod> [--item=<item>] --<field>=<value> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 43 |  | View Code Duplication | 	function save( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$pod  = $assoc_args['pod']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$item = pods_var_raw( 'item', $assoc_args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		unset( $assoc_args['pod'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		if ( isset( $assoc_args['item'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			unset( $assoc_args['item'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		if ( ! empty( $assoc_args ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			$id = pods( $pod, $item )->save( $assoc_args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if ( 0 < $id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				WP_CLI::success( __( 'Pod item saved', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 				WP_CLI::line( "ID: {$id}" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				WP_CLI::error( __( 'Error saving pod item', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			WP_CLI::error( __( 'No data sent for saving', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @synopsis --pod=<pod> --item=<item> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | 	 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 74 |  | View Code Duplication | 	function duplicate( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		$id = pods( $assoc_args['pod'], $assoc_args['item'] )->duplicate(); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		if ( 0 < $id ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 			WP_CLI::success( __( 'Pod item duplicated', 'pods' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 			WP_CLI::line( "New ID: {$id}" ); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 			WP_CLI::error( __( 'Error duplicating pod item', 'pods' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @synopsis --pod=<pod> --item=<item> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	function delete( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$deleted = pods( $assoc_args['pod'], $assoc_args['item'] )->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		if ( $deleted ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			WP_CLI::success( __( 'Pod item deleted', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			WP_CLI::error( __( 'Error deleting pod item', 'pods' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @synopsis --pod=<pod> --file=<file> [--item=<item>] [--format=<format>] [--fields=<fields>] [--depth=<depth>] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/*function export ( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			'fields' => pods_var_raw( 'fields', $assoc_args ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			'depth' => pods_var_raw( 'depth', $assoc_args ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$data = pods( $assoc_args[ 'pod' ], $assoc_args[ 'item' ] )->export( $params, null, pods_var_raw( 'format', $assoc_args ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		// @todo write to file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		// @todo success message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	}*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @synopsis --pod=<pod> --file=<file> [--format=<format>] [--numeric-mode=<numeric_mode>] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	/*function import ( $args, $assoc_args ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$data = array(); // @todo get data from file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$ids = pods_api( $assoc_args[ 'pod' ] )->import( $data, (boolean) pods_var_raw( 'numeric_mode', $assoc_args, false, null, true ), pods_var_raw( 'format', $assoc_args ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		// @todo success message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	}*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | WP_CLI::add_command( 'pods', 'Pods_CLI_Command' ); | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.