├── .travis.yml ├── README.MD ├── bin └── install-wp-tests.sh ├── classes ├── class-pods-api-request-params.php ├── class-pods-rest-api-controller.php ├── class-pods-rest-api-response-controller.php ├── class-pods-rest-api.php └── routes │ ├── class-pods-rest-api-route-pods.php │ └── class-pods-rest-api-route-podsapi.php ├── docs ├── authentication │ └── index.md ├── contributing.md ├── contributing │ ├── gitflow.md │ └── naming-conventions.txt ├── filters │ └── index.md └── routes │ └── index.md ├── includes └── functions.php ├── license.txt ├── phpunit.xml.dist ├── pods-rest-api.php └── tests ├── bootstrap.php ├── routes ├── class-pods-rests-api-test-routes-pods.php └── class-pods-rests-api-test-routes-podsapi.php ├── tests-pods-rest-api.php └── tests-request-params.php /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | env: 11 | - WP_VERSION=latest WP_MULTISITE=0 12 | - WP_VERSION=latest WP_MULTISITE=1 13 | - WP_VERSION=4.2 WP_MULTISITE=0 14 | - WP_VERSION=4.2 WP_MULTISITE=1 15 | - WP_VERSION=4.1 WP_MULTISITE=0 16 | - WP_VERSION=4.1 WP_MULTISITE=1 17 | - WP_VERSION=4.0 WP_MULTISITE=0 18 | - WP_VERSION=4.0 WP_MULTISITE=1 19 | - WP_VERSION=3.9 WP_MULTISITE=0 20 | - WP_VERSION=3.9 WP_MULTISITE=1 21 | - WP_VERSION=3.8 WP_MULTISITE=0 22 | - WP_VERSION=3.8 WP_MULTISITE=1 23 | 24 | 25 | before_script: 26 | - travis_retry composer self-update 27 | - travis_retry composer install --no-interaction --prefer-source 28 | - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION 29 | - ln -s . /tmp/wordpress/wp-content/plugins/pods 30 | 31 | script: 32 | - vendor/bin/phpunit 33 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | Pods REST API 2 | ========== 3 | 4 | This plugin was a neat experiment. Functionality is now migrating into Pods core, beginning with Pods 5 | 2.6. See: 6 | 7 | * https://github.com/pods-framework/pods/issues/3209 8 | * https://github.com/pods-framework/pods/issues/3182 9 | * https://github.com/pods-framework/pods/pull/3184 10 | -------------------------------------------------------------------------------- /bin/install-wp-tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ $# -lt 3 ]; then 4 | echo "usage: $0 [db-host] [wp-version]" 5 | exit 1 6 | fi 7 | 8 | DB_NAME=$1 9 | DB_USER=$2 10 | DB_PASS=$3 11 | DB_HOST=${4-localhost} 12 | WP_VERSION=${5-latest} 13 | 14 | WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} 15 | WP_CORE_DIR=/tmp/wordpress/ 16 | 17 | set -ex 18 | 19 | install_wp() { 20 | mkdir -p $WP_CORE_DIR 21 | 22 | if [ $WP_VERSION == 'latest' ]; then 23 | local ARCHIVE_NAME='latest' 24 | else 25 | local ARCHIVE_NAME="wordpress-$WP_VERSION" 26 | fi 27 | 28 | wget -nv -O /tmp/wordpress.tar.gz https://wordpress.org/${ARCHIVE_NAME}.tar.gz 29 | tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR 30 | 31 | wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php 32 | } 33 | 34 | install_test_suite() { 35 | # portable in-place argument for both GNU sed and Mac OSX sed 36 | if [[ $(uname -s) == 'Darwin' ]]; then 37 | local ioption='-i .bak' 38 | else 39 | local ioption='-i' 40 | fi 41 | 42 | # set up testing suite 43 | mkdir -p $WP_TESTS_DIR 44 | cd $WP_TESTS_DIR 45 | svn co --quiet https://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ 46 | 47 | wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php 48 | sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php 49 | sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php 50 | sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php 51 | sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php 52 | sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php 53 | } 54 | 55 | install_db() { 56 | # parse DB_HOST for port or socket references 57 | local PARTS=(${DB_HOST//\:/ }) 58 | local DB_HOSTNAME=${PARTS[0]}; 59 | local DB_SOCK_OR_PORT=${PARTS[1]}; 60 | local EXTRA="" 61 | 62 | if ! [ -z $DB_HOSTNAME ] ; then 63 | if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then 64 | EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" 65 | elif ! [ -z $DB_SOCK_OR_PORT ] ; then 66 | EXTRA=" --socket=$DB_SOCK_OR_PORT" 67 | elif ! [ -z $DB_HOSTNAME ] ; then 68 | EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" 69 | fi 70 | fi 71 | 72 | # create database 73 | mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA 74 | } 75 | 76 | 77 | install_pods() { 78 | git clone https://github.com/pods-framework/pods $WP_CORE_DIR/wp-content/plugins/pods 79 | cd $WP_CORE_DIR/wp-content/plugins/pods 80 | git checkout 2.x 81 | 82 | } 83 | 84 | install_rest_api() { 85 | git clone https://github.com/WP-API/WP-API $WP_CORE_DIR/wp-content/plugins/json-rest-api 86 | cd $WP_CORE_DIR/wp-content/plugins/json-rest-api 87 | git checkout develop 88 | 89 | } 90 | 91 | install_wp 92 | install_pods 93 | install_rest_api 94 | install_test_suite 95 | install_db 96 | -------------------------------------------------------------------------------- /classes/class-pods-api-request-params.php: -------------------------------------------------------------------------------- 1 | '=', 21 | 'is' => '=', 22 | 'isnot' => '!=', 23 | 'isin' => 'IN', 24 | 'isnotin' => 'NOT IN', 25 | 'greater' => '>', 26 | 'smaller' => '<', 27 | 'greatereq' => '>=', 28 | 'smallereq' => '<=', 29 | 'contains' => 'LIKE', 30 | ); 31 | 32 | /** 33 | * Build params array from request data. 34 | * 35 | * @since 0.0.1 36 | * 37 | * @param array $data Request data. 38 | * @param string $pod_name Name of Pod 39 | * @param string $method Transfer method being used. 40 | * 41 | * @return array|int 42 | */ 43 | public static function build_params( $data, $pod_name, $method ) { 44 | $params = array(); 45 | if ( ! isset( $data['id'] ) ) { 46 | $params = self::page( $data, $params ); 47 | $params = self::limit( $data, $params ); 48 | $params = self::where( $data, $params ); 49 | $params = self::orderby( $data, $params ); 50 | $params = array( $params ); 51 | 52 | 53 | } else { 54 | $params = absint( $data['id'] ); 55 | 56 | } 57 | 58 | //@todo a filter here for adding additional query 59 | //will use $pod_name & $method 60 | return $params; 61 | } 62 | 63 | /** 64 | * Add where clause to $params 65 | * 66 | * @since 0.0.1 67 | * 68 | * @param array $data Request data 69 | * @param array $params Optional. Query params to add to. Default is an empty array. 70 | * 71 | * @return array 72 | */ 73 | public static function where( $data, $params = array() ) { 74 | 75 | if ( ! is_null( $where = pods_v_sanitized( 'where', $data ) ) ) { 76 | $_where = array( 77 | 'key' => pods_v( 'key', $where ), 78 | 'value' => pods_v( 'value', $where ), 79 | 'compare' => pods_v( 'compare', $where ) 80 | ); 81 | 82 | $_where['compare'] = self::comparison_translate( $_where['compare'] ); 83 | 84 | if ( array_filter( $_where ) ) { 85 | $params['where'] = $_where; 86 | 87 | return $params; 88 | } 89 | 90 | return $params; 91 | 92 | } 93 | 94 | return $params; 95 | 96 | } 97 | 98 | /** 99 | * Add orderby clause to $params 100 | * 101 | * @since 0.0.1 102 | * 103 | * @param array $data Request data 104 | * @param array $params Optional. Query params to add to. Default is an empty array. 105 | * 106 | * @return array 107 | */ 108 | public static function orderby( $data, $params = array() ) { 109 | if ( ! is_null( $order = pods_v_sanitized( 'orderby', $data ) ) ) { 110 | if ( ! is_null( $field = pods_v( 'field', $order ) ) && ( ! is_null( $dir = pods_v( 'direction', $order ) ) && in_array( $dir, array( 111 | 'ASC', 112 | 'DESC' 113 | ) ) ) 114 | ) { 115 | 116 | $_orderby = sprintf( '%1s %2s', $field, $dir ); 117 | 118 | if ( pods_v( 'cast', $order ) && ! is_null( $cast_as = pods_v( 'cast_as', $order ) ) ) { 119 | $_orderby = sprintf( 'CAST( %1s AS %2s)', $_orderby, $cast_as ); 120 | } 121 | 122 | if ( is_string( $_orderby ) ) { 123 | $params['orderby'] = $_orderby; 124 | 125 | return $params; 126 | } 127 | 128 | return $params; 129 | 130 | } 131 | 132 | return $params; 133 | 134 | } 135 | 136 | return $params; 137 | } 138 | 139 | /** 140 | * Add limit clause to $params 141 | * 142 | * @since 0.0.1 143 | * 144 | * @param array $data Request data 145 | * @param array $params Optional. Query params to add to. Default is an empty array. 146 | * 147 | * @return array 148 | */ 149 | public static function limit( $data, $params = array() ) { 150 | $params['limit'] = pods_v( 'limit', $data, 15 ); 151 | 152 | return $params; 153 | } 154 | 155 | /** 156 | * Add page clause to $params 157 | * 158 | * @since 0.0.1 159 | * 160 | * @param array $data Request data 161 | * @param array $params Optional. Query params to add to. Default is an empty array. 162 | * 163 | * @return array 164 | */ 165 | public static function page( $data, $params = array() ) { 166 | if ( ! $page = is_null( pods_v( 'page', $data ) ) ) { 167 | $params['page'] = $page; 168 | 169 | return $params; 170 | } 171 | 172 | return $params; 173 | 174 | } 175 | 176 | /** 177 | * Translates URL friendly comparisons to actual SQL operators 178 | * 179 | * @since 0.0.1 180 | * 181 | * @param string $comparison Comparison, must be in self::$comparisons 182 | * 183 | * @return string|void The operator if input was legal. 184 | */ 185 | public static function comparison_translate( $comparison ) { 186 | $comparison = strtolower( $comparison ); 187 | if ( array_key_exists( $comparison, self::$comparisons ) ) { 188 | return self::$comparisons[ $comparison ]; 189 | } 190 | 191 | } 192 | 193 | } 194 | -------------------------------------------------------------------------------- /classes/class-pods-rest-api-controller.php: -------------------------------------------------------------------------------- 1 | route_url = $route; 32 | $this->register_routes(); 33 | global $wp_json_server; 34 | 35 | $this->wp_json_server = $wp_json_server; 36 | 37 | } 38 | 39 | /** 40 | * Automatically register the routes for this endpoint 41 | * 42 | * @todo all routes 43 | * 44 | * @since 0.0.1 45 | */ 46 | public function register_routes() { 47 | $pods = pods_api()->load_pods( array( 'key_names' => true ) ); 48 | foreach ( $pods as $pod => $data ) { 49 | $url = trailingslashit( $this->route_url ) . $pod; 50 | register_json_route( PODS_REST_API_BASE_URL, $url, 51 | array( 52 | array( 53 | 'methods' => WP_JSON_Server::READABLE, 54 | 'callback' => array( $this, 'get_items' ), 55 | 'args' => array( 56 | 'context' => array( 57 | 'default' => 'view', 58 | ), 59 | 'type' => array(), 60 | 'page' => array(), 61 | ), 62 | 'permission_callback' => array( $this, 'permissions_check' ), 63 | ), 64 | 65 | ) 66 | 67 | ); 68 | $url = trailingslashit( $this->route_url ) . $pod . '/(?P[\d]+)'; 69 | register_json_route( PODS_REST_API_BASE_URL, $url, 70 | array( 71 | 'methods' => WP_JSON_Server::READABLE, 72 | 'callback' => array( $this, 'get_item' ), 73 | 'permission_callback' => array( $this, 'permissions_check' ), 74 | 'args' => array( 75 | 'context' => array( 76 | 'default' => 'view', 77 | ), 78 | ), 79 | ) 80 | ); 81 | } 82 | 83 | } 84 | 85 | 86 | /** 87 | * Placeholder method! 88 | * 89 | * @return bool 90 | */ 91 | public function permissions_check() { 92 | return true; 93 | } 94 | /** 95 | * Placeholder method! 96 | * 97 | * @return object 98 | */ 99 | public function error( $message, $data = array(), $return_partial = true ) { 100 | $error = new stdClass(); 101 | $error->code = 500; 102 | $error->message = $message; 103 | if ( $return_partial ) { 104 | $error->data = $data; 105 | }else{ 106 | $error->data = $message; 107 | } 108 | 109 | 110 | return $error; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /classes/class-pods-rest-api-response-controller.php: -------------------------------------------------------------------------------- 1 | pod = $pod; 29 | } 30 | 31 | /** 32 | * Send navigation-related headers with Pods responses 33 | * 34 | * @param \WP_Query|Object $query Not used. Included for sake of strict standardss 35 | * 36 | */ 37 | public function query_navigation_headers( $query ) { 38 | $max_page = ceil( $this->pod->total_found() / $this->pod->limit ); 39 | 40 | $this->header( 'X-WP-Total', $this->pod->total_found() ); 41 | $this->header( 'X-WP-TotalPages', $max_page ); 42 | 43 | do_action( 'json_query_navigation_headers', $this, $this->pod ); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /classes/class-pods-rest-api.php: -------------------------------------------------------------------------------- 1 | get_params(); 31 | $id = absint( pods_v( 'id', $request->get_url_params() ) ); 32 | $route = $request->get_route(); 33 | $parse_route = explode( '/', $route ); 34 | end( $parse_route); 35 | $last = key($parse_route ); 36 | $method = $request->get_method(); 37 | 38 | if ( 0 < (int) $id ) { 39 | $single_item = true; 40 | $params = $id; 41 | $key = $last - 1; 42 | $pod_name = $parse_route[ $key ]; 43 | }else{ 44 | $single_item = false; 45 | $pod_name = $parse_route[ $last ]; 46 | $params = Pods_REST_API_Request_Params::build_params( $data, $pod_name, $method ); 47 | 48 | } 49 | 50 | $headers = array(); 51 | if ( ! empty( $params ) ) { 52 | $pods = pods( $pod_name, $params ); 53 | 54 | if ( $single_item ) { 55 | //@todo replace with a way to get fields based on config 56 | $response[ (int) $pods->id() ] = $pods->export(); 57 | } else { 58 | if ( 0 < $pods->total() ) { 59 | while ( $pods->fetch() ) { 60 | //@todo replace with a way to get fields based on config 61 | $response[ (int) $pods->id() ] = $pods->export(); 62 | } 63 | } else { 64 | $response = array(); 65 | } 66 | } 67 | 68 | $status = 200; 69 | }else{ 70 | $error = $this->error( __( sprintf( 'Bad params in %1s', __METHOD__ ), 'pods-rest-api' ) ); 71 | $response = $error->message; 72 | $status = $response->code; 73 | } 74 | 75 | $response = pods_rest_api_response( $pods, $response, $status, $headers ); 76 | 77 | return $response; 78 | 79 | } 80 | 81 | } 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /classes/routes/class-pods-rest-api-route-podsapi.php: -------------------------------------------------------------------------------- 1 | /feature/. 6 | * A pull request, with the original issue in the pull request comment, should be made against the current development branch in order for tests to be run and the branch to be considered for merging. 7 | * Failure to comply with this strategy is a violation of intergalactic law and 8 | -------------------------------------------------------------------------------- /docs/contributing/naming-conventions.txt: -------------------------------------------------------------------------------- 1 | Plugin Name -- Pods REST API 2 | Text domain -- pods-rest-api 3 | Function prefix -- pods_rest_api 4 | Class prefix -- Pods_REST_API 5 | Root namespace -- n/a 6 | Hook Prefix -- pods_rest_api 7 | -------------------------------------------------------------------------------- /docs/filters/index.md: -------------------------------------------------------------------------------- 1 | // 2 | -------------------------------------------------------------------------------- /docs/routes/index.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Default Routes 4 | 5 | ### Disabling Default Routes 6 | The default routes can be disabled by defining the constant `PODS_REST_API_ENABLE_DEFAULT_ROUTES` as false. 7 | -------------------------------------------------------------------------------- /includes/functions.php: -------------------------------------------------------------------------------- 1 | set_pod( $pod ); 12 | 13 | return $response; 14 | } 15 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 6 | 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your 13 | freedom to share and change it. By contrast, the GNU General Public 14 | License is intended to guarantee your freedom to share and change free 15 | software--to make sure the software is free for all its users. This 16 | General Public License applies to most of the Free Software 17 | Foundation's software and to any other program whose authors commit to 18 | using it. (Some other Free Software Foundation software is covered by 19 | the GNU Library General Public License instead.) You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | this service if you wish), that you receive source code or can get it 26 | if you want it, that you can change the software or use pieces of it 27 | in new free programs; and that you know you can do these things. 28 | 29 | To protect your rights, we need to make restrictions that forbid 30 | anyone to deny you these rights or to ask you to surrender the rights. 31 | These restrictions translate to certain responsibilities for you if you 32 | distribute copies of the software, or if you modify it. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must give the recipients all the rights that 36 | you have. You must make sure that they, too, receive or can get the 37 | source code. And you must show them these terms so they know their 38 | rights. 39 | 40 | We protect your rights with two steps: (1) copyright the software, and 41 | (2) offer you this license which gives you legal permission to copy, 42 | distribute and/or modify the software. 43 | 44 | Also, for each author's protection and ours, we want to make certain 45 | that everyone understands that there is no warranty for this free 46 | software. If the software is modified by someone else and passed on, we 47 | want its recipients to know that what they have is not the original, so 48 | that any problems introduced by others will not reflect on the original 49 | authors' reputations. 50 | 51 | Finally, any free program is threatened constantly by software 52 | patents. We wish to avoid the danger that redistributors of a free 53 | program will individually obtain patent licenses, in effect making the 54 | program proprietary. To prevent this, we have made it clear that any 55 | patent must be licensed for everyone's free use or not licensed at all. 56 | 57 | The precise terms and conditions for copying, distribution and 58 | modification follow. 59 | 60 | GNU GENERAL PUBLIC LICENSE 61 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 62 | 63 | 0. This License applies to any program or other work which contains 64 | a notice placed by the copyright holder saying it may be distributed 65 | under the terms of this General Public License. The "Program", below, 66 | refers to any such program or work, and a "work based on the Program" 67 | means either the Program or any derivative work under copyright law: 68 | that is to say, a work containing the Program or a portion of it, 69 | either verbatim or with modifications and/or translated into another 70 | language. (Hereinafter, translation is included without limitation in 71 | the term "modification".) Each licensee is addressed as "you". 72 | 73 | Activities other than copying, distribution and modification are not 74 | covered by this License; they are outside its scope. The act of 75 | running the Program is not restricted, and the output from the Program 76 | is covered only if its contents constitute a work based on the 77 | Program (independent of having been made by running the Program). 78 | Whether that is true depends on what the Program does. 79 | 80 | 1. You may copy and distribute verbatim copies of the Program's 81 | source code as you receive it, in any medium, provided that you 82 | conspicuously and appropriately publish on each copy an appropriate 83 | copyright notice and disclaimer of warranty; keep intact all the 84 | notices that refer to this License and to the absence of any warranty; 85 | and give any other recipients of the Program a copy of this License 86 | along with the Program. 87 | 88 | You may charge a fee for the physical act of transferring a copy, and 89 | you may at your option offer warranty protection in exchange for a fee. 90 | 91 | 2. You may modify your copy or copies of the Program or any portion 92 | of it, thus forming a work based on the Program, and copy and 93 | distribute such modifications or work under the terms of Section 1 94 | above, provided that you also meet all of these conditions: 95 | 96 | a) You must cause the modified files to carry prominent notices 97 | stating that you changed the files and the date of any change. 98 | 99 | b) You must cause any work that you distribute or publish, that in 100 | whole or in part contains or is derived from the Program or any 101 | part thereof, to be licensed as a whole at no charge to all third 102 | parties under the terms of this License. 103 | 104 | c) If the modified program normally reads commands interactively 105 | when run, you must cause it, when started running for such 106 | interactive use in the most ordinary way, to print or display an 107 | announcement including an appropriate copyright notice and a 108 | notice that there is no warranty (or else, saying that you provide 109 | a warranty) and that users may redistribute the program under 110 | these conditions, and telling the user how to view a copy of this 111 | License. (Exception: if the Program itself is interactive but 112 | does not normally print such an announcement, your work based on 113 | the Program is not required to print an announcement.) 114 | 115 | These requirements apply to the modified work as a whole. If 116 | identifiable sections of that work are not derived from the Program, 117 | and can be reasonably considered independent and separate works in 118 | themselves, then this License, and its terms, do not apply to those 119 | sections when you distribute them as separate works. But when you 120 | distribute the same sections as part of a whole which is a work based 121 | on the Program, the distribution of the whole must be on the terms of 122 | this License, whose permissions for other licensees extend to the 123 | entire whole, and thus to each and every part regardless of who wrote it. 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | ./tests/ 12 | 13 | 14 | 15 | 16 | ./routes 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /pods-rest-api.php: -------------------------------------------------------------------------------- 1 | =' ) ) { 87 | $fail[] = __( sprintf( 'Pods REST API requires Pods version %1s or later. Current version is %2s.', PODS_REST_API_MIN_PODS_VERSION, PODS_VERSION ), 'pods-rest-api' ); 88 | } 89 | 90 | if ( ! version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { 91 | $fail[] = __( sprintf( 'Pods REST API requires PHP version %1s or later. Current version is %2s.', '5.3.0', PHP_VERSION ), 'pods-rest-api' ); 92 | 93 | } 94 | } 95 | 96 | 97 | 98 | if ( is_array( $fail ) ) { 99 | 100 | if ( is_admin() ) { 101 | foreach ( $fail as $message ) { 102 | echo sprintf( '

%s

', 103 | $message 104 | ); 105 | } 106 | } 107 | 108 | return false; 109 | 110 | } 111 | 112 | include_once( dirname( __FILE__ ) . '/classes/class-pods-rest-api.php' ); 113 | include_once( dirname( __FILE__ ) . '/classes/class-pods-rest-api-controller.php' ); 114 | include_once( dirname( __FILE__ ) . '/classes/class-pods-rest-api-response-controller.php' ); 115 | include_once( dirname( __FILE__ ) . '/classes/class-pods-api-request-params.php' ); 116 | include_once( dirname( __FILE__ ) . '/includes/functions.php' ); 117 | 118 | new Pods_REST_API(); 119 | 120 | } 121 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | server = new WP_JSON_Server; 19 | $this->class = new Pods_REST_API_Route_Pods( 'pods' ); 20 | 21 | $pod_name = 'frogs'; 22 | $params = array( 23 | 'storage' => 'meta', 24 | 'type' => 'post_type', 25 | 'name' => $pod_name, 26 | ); 27 | 28 | $this->$pod_id = pods_api()->save_pod( $params ); 29 | 30 | if ( $pod_id ) { 31 | $params = array( 32 | 'pod_id' => $pod_id, 33 | 'pod' => $pod_name, 34 | 'name' => 'text_test', 35 | 'type' => 'text', 36 | ); 37 | $field_id = pods_api()->save_field ( $params ); 38 | 39 | } 40 | 41 | $this->pod = pods( $pod_name ); 42 | $data = array( 43 | 'text_test' => 'foo', 44 | 'post_title' => 'one' 45 | ); 46 | $this->item_one_id = $this->pod->save( $data ); 47 | 48 | $this->pod->reset(); 49 | 50 | $data = array( 51 | 'text_test' => 'bar', 52 | 'post_title' => 'two' 53 | ); 54 | $this->item_two_id = $this->pod->save( $data ); 55 | 56 | 57 | } 58 | 59 | /** 60 | * Tear down 61 | * 62 | * @since 0.1.0 63 | */ 64 | function tearDown() { 65 | parent::tearDown(); 66 | } 67 | 68 | public function test_that_tests_work() { 69 | $this->assertEquals( 1, 1); 70 | } 71 | 72 | /** 73 | * Test that a where query gets the right post only 74 | * 75 | * @since 0.0.1 76 | * 77 | * @covers Pods_REST_API_Route_Pods::get_items 78 | * @covers Pods_REST_API_Route_Pods::__call 79 | */ 80 | public function test_get_items_where() { 81 | $request = new WP_JSON_Request( 'GET', '/pods/pods/frogs' ); 82 | $request->set_query_params( array( 83 | 'where' => array( 84 | 'key' => 'foo', 85 | 'value' => 'bar', 86 | 'comparison' => 'equals' 87 | ) 88 | ) ); 89 | $response = $this->server->dispatch( $request ); 90 | 91 | $this->assertNotInstanceOf( 'WP_Error', $response ); 92 | $response = json_ensure_response( $response ); 93 | $this->assertEquals( 200, $response->get_status() ); 94 | 95 | $data = $response->get_data(); 96 | 97 | $this->assertArrayHasKey( $this->item_one_id, $data ); 98 | $this->assertArrayNotHasKey( $this->item_two_id, $data ); 99 | 100 | 101 | } 102 | 103 | /** 104 | * Test that a orderby query orders right 105 | * 106 | * @since 0.0.1 107 | * 108 | * @covers Pods_REST_API_Route_Pods::get_items 109 | * * @covers Pods_REST_API_Route_Pods::__call 110 | */ 111 | public function test_get_items_orderby() { 112 | $request = new WP_JSON_Request( 'GET', '/pods/pods/frogs' ); 113 | $request->set_query_params( array( 114 | 'orderby' => array( 115 | 'field' => 'text_test.metavalue', 116 | 'direction' => 'ASC', 117 | ) 118 | ) ); 119 | $response = $this->server->dispatch( $request ); 120 | 121 | $this->assertNotInstanceOf( 'WP_Error', $response ); 122 | $response = json_ensure_response( $response ); 123 | $this->assertEquals( 200, $response->get_status() ); 124 | 125 | $data = $response->get_data(); 126 | 127 | reset( $data ); 128 | $first_key = key( $data ); 129 | end( $data ); 130 | $last_key = key( $data ); 131 | 132 | 133 | $this->assertEquals( $this->item_two_id, $first_key ); 134 | $this->assertEquals( $this->item_one_id, $last_key ); 135 | 136 | } 137 | 138 | /** 139 | * Test that we can query by ID 140 | * 141 | * @since 0.0.1 142 | * 143 | * @covers Pods_REST_API_Route_Pods::get_item 144 | * @covers Pods_REST_API_Route_Pods::__call 145 | */ 146 | public function test_get_item() { 147 | $request = new WP_JSON_Request( 'GET', '/pods/pods/frogs' . $this->item_one_id ); 148 | $request->set_query_params( array( 149 | ) ); 150 | $response = $this->server->dispatch( $request ); 151 | 152 | $this->assertNotInstanceOf( 'WP_Error', $response ); 153 | $response = json_ensure_response( $response ); 154 | $this->assertEquals( 200, $response->get_status() ); 155 | 156 | $data = $response->get_data(); 157 | 158 | $this->assertArrayHasKey( $this->item_one_id, $data ); 159 | $this->assertArrayNotHasKey( $this->item_two_id, $data ); 160 | 161 | } 162 | 163 | /** 164 | * Test that we can create an item 165 | * 166 | * @since 0.0.1 167 | * 168 | * @covers Pods_REST_API_Route_Pods::create_item 169 | * @covers Pods_REST_API_Route_Pods::__call 170 | */ 171 | public function test_create_item() { 172 | $request = new WP_JSON_Request( 'POST', '/pods/pods/frogs' ); 173 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 174 | $params = array( 175 | 'post_title' => 'dogs', 176 | 'test_text' => 'cats' 177 | ); 178 | 179 | $request->set_body_params( $params ); 180 | 181 | $response = $this->server->dispatch( $request ); 182 | 183 | $this->assertNotInstanceOf( 'WP_Error', $response ); 184 | $response = json_ensure_response( $response ); 185 | 186 | $this->assertEquals( 201, $response->get_status() ); 187 | 188 | $data = $response->get_data(); 189 | 190 | $this->assertEquals( $data[ 'post_title' ], 'dogs' ); 191 | $this->assertEquals( $data[ 'test_text' ], 'cats' ); 192 | $pods = pods( 'frogs', $data['id'] ); 193 | 194 | 195 | $this->assertEquals( $pods->id(), $data['id'] ); 196 | $this->assertEquals( $pods->display( 'post_title' ), 'dogs' ); 197 | $this->assertEquals( $pods->display( 'test_text' ), 'cats' ); 198 | 199 | } 200 | 201 | /** 202 | * Test that we can update an item 203 | * 204 | * @since 0.0.1 205 | * 206 | * @covers Pods_REST_API_Route_Pods::update_item 207 | * @covers Pods_REST_API_Route_Pods::__call 208 | */ 209 | public function test_update_item() { 210 | $request = new WP_JSON_Request( 'POST', '/pods/pods/frogs' . $this->item_one_id ); 211 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 212 | $params = array( 213 | 'post_title' => 'dogs', 214 | 'test_text' => 'cats' 215 | ); 216 | 217 | $request->set_body_params( $params ); 218 | 219 | $response = $this->server->dispatch( $request ); 220 | 221 | $this->assertNotInstanceOf( 'WP_Error', $response ); 222 | $response = json_ensure_response( $response ); 223 | 224 | $this->assertEquals( 201, $response->get_status() ); 225 | 226 | $data = $response->get_data(); 227 | 228 | $this->assertEquals( $data[ 'post_title' ], 'dogs' ); 229 | $this->assertEquals( $data[ 'test_text' ], 'cats' ); 230 | $pods = pods( 'frogs', $data['id'] ); 231 | 232 | $this->assertEquals( $data['id'], $pods->id() ); 233 | $this->assertEquals( $pods->display( 'post_title' ), 'dogs' ); 234 | $this->assertEquals( $pods->display( 'test_text' ), 'cats' ); 235 | 236 | } 237 | 238 | /** 239 | * Test that we can delete an item 240 | * 241 | * @since 0.0.1 242 | * 243 | * @covers Pods_REST_API_Route_Pods::delete_item 244 | * @covers Pods_REST_API_Route_Pods::__call 245 | */ 246 | public function test_delete_item() { 247 | $request = new WP_JSON_Request( 'DELETE', '/pods/pods/frogs' . $this->item_one_id ); 248 | 249 | 250 | $response = $this->server->dispatch( $request ); 251 | 252 | $this->assertNotInstanceOf( 'WP_Error', $response ); 253 | $response = json_ensure_response( $response ); 254 | 255 | $this->assertEquals( 200, $response->get_status() ); 256 | 257 | 258 | $pods = pods( 'frogs', $this->item_one_id ); 259 | 260 | $this->assertEquals( 0, $pods->total() ); 261 | $this->assertNotEquals( $this->item_one_id, $pods->id() ); 262 | 263 | } 264 | 265 | /** 266 | * Test "pods_rest_api_return_fields" filter 267 | * 268 | * @since 0.0.1 269 | */ 270 | public function test_filter_return_fields() { 271 | add_filter( 'pods_rest_api_return_fields', function( $return ) { 272 | return array( 273 | 'post_title' 274 | ); 275 | }); 276 | 277 | $request = new WP_JSON_Request( 'GET', '/pods/pods/frogs' . $this->item_one_id ); 278 | $request->set_query_params( array( 279 | ) ); 280 | $response = $this->server->dispatch( $request ); 281 | 282 | $this->assertNotInstanceOf( 'WP_Error', $response ); 283 | $response = json_ensure_response( $response ); 284 | $this->assertEquals( 200, $response->get_status() ); 285 | 286 | $data = $response->get_data(); 287 | 288 | $this->assertArrayHasKey( 'post_title', $data ); 289 | $this->assertArrayNotHasKey( 'post_excerpt', $data ); 290 | $this->assertArrayNotHasKey( 'text_text', $data ); 291 | 292 | } 293 | 294 | /** 295 | * Test "pods_rest_api_default_query" filter 296 | * 297 | * @since 0.0.1 298 | */ 299 | public function test_filter_default_query() { 300 | add_filter( 'pods_rest_api_default_query',function( $query ) { 301 | $query[ 'limit' ] = 1; 302 | 303 | return $query; 304 | }); 305 | 306 | $request = new WP_JSON_Request( 'GET', '/pods/pods/frogs' ); 307 | $request->set_query_params( array( 308 | 'limit' => 7 309 | ) ); 310 | $response = $this->server->dispatch( $request ); 311 | 312 | $this->assertNotInstanceOf( 'WP_Error', $response ); 313 | $response = json_ensure_response( $response ); 314 | $this->assertEquals( 200, $response->get_status() ); 315 | 316 | $data = $response->get_data(); 317 | $this->assertSame( 1, count( $data ) ); 318 | 319 | } 320 | 321 | } 322 | -------------------------------------------------------------------------------- /tests/routes/class-pods-rests-api-test-routes-podsapi.php: -------------------------------------------------------------------------------- 1 | 'table', 22 | 'type' => 'pod', 23 | 'name' => $pod_name, 24 | ); 25 | 26 | $this->pod_one_id = pods_api()->save_pod( $params ); 27 | 28 | if ( $this->pod_one_id ) { 29 | $params = array( 30 | 'pod_id' => $this->pod_one_id, 31 | 'pod' => $pod_name, 32 | 'name' => 'text_test', 33 | 'type' => 'text', 34 | ); 35 | $field_id = pods_api()->save_field ( $params ); 36 | 37 | } 38 | 39 | $pod_name = 'toads'; 40 | $params = array( 41 | 'storage' => 'table', 42 | 'type' => 'pod', 43 | 'name' => $pod_name, 44 | ); 45 | 46 | $this->pod_two_id = pods_api()->save_pod( $params ); 47 | 48 | if ( $this->pod_two_id ) { 49 | $params = array( 50 | 'pod_id' => $this->$pod_one_id, 51 | 'pod' => $pod_name, 52 | 'name' => 'text_test', 53 | 'type' => 'text', 54 | ); 55 | $field_id = pods_api()->save_field ( $params ); 56 | 57 | } 58 | 59 | 60 | 61 | } 62 | 63 | /** 64 | * Tear down 65 | * 66 | * @since 0.1.0 67 | */ 68 | function tearDown() { 69 | parent::tearDown(); 70 | } 71 | 72 | /** 73 | * Test that tests work 74 | * 75 | * @since 0.0.1 76 | * 77 | * @covers Josh 78 | */ 79 | public function test_that_tests_work() { 80 | $this->assertEquals( 1, 1 ); 81 | } 82 | 83 | /** 84 | * Test that we can get data about active Pods 85 | * 86 | * @since 0.0.1 87 | * 88 | * @covers Pods_JSON_API_Pods_API::get_pods 89 | */ 90 | public function test_get_pods() { 91 | $request = new WP_JSON_Request( 'GET', '/pods/pods-api' ); 92 | $response = $this->server->dispatch( $request ); 93 | $this->assertNotInstanceOf( 'WP_Error', $response ); 94 | $response = json_ensure_response( $response ); 95 | $this->assertEquals( 200, $response->get_status() ); 96 | $data = $response->get_data(); 97 | 98 | $this->assertEquals( 2, count( $data ) ); 99 | 100 | //assuming that new version will key be Pod ID 101 | $this->assertArrayHasKey( $this->pod_one_id, $data ); 102 | $this->assertArrayHasKey( $this->pod_one_two, $data ); 103 | 104 | $this->assertArrayHasKey( $data[ $this->pod_one_id ], 'name' ); 105 | $this->assertEquals( $data[ $this->pod_one_id ][ 'name'], 'frogs' ); 106 | 107 | $this->assertArrayHasKey( $data[ $this->pod_two_id ], 'name' ); 108 | $this->assertEquals( $data[ $this->pod_two_id ][ 'name' ], 'toads' ); 109 | 110 | //@todo add response filter and try again 111 | //?? diffrent test 112 | 113 | } 114 | 115 | /** 116 | * Test that we can get data about active Pods and limit returned data properly. 117 | * 118 | * @since 0.0.1 119 | * 120 | * @covers Pods_JSON_API_Pods_API::get_pod 121 | */ 122 | public function test_get_pod() { 123 | $request = new WP_JSON_Request( 'GET', '/pods/pods-api/frogs' ); 124 | $response = $this->server->dispatch( $request ); 125 | $this->assertNotInstanceOf( 'WP_Error', $response ); 126 | $response = json_ensure_response( $response ); 127 | $this->assertEquals( 200, $response->get_status() ); 128 | $data = $response->get_data(); 129 | 130 | $this->assertArrayHasKey( $data, 'name' ); 131 | $this->assertEquals( $data[ 'name' ], 'frogs' ); 132 | 133 | //@todo add response filter and try again 134 | //?? diffrent test 135 | 136 | } 137 | 138 | /** 139 | * Test that we can add a Pod 140 | * 141 | * @since 0.0.1 142 | * 143 | * @covers Pods_JSON_API_Pods_API::add_pod 144 | */ 145 | public function test_add_pod() { 146 | $request = new WP_JSON_Request( 'POST', '/pods/pods-api' ); 147 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 148 | $params = array( 149 | 'storage' => 'table', 150 | 'type' => 'pod', 151 | 'name' => 'lizards' 152 | ); 153 | 154 | $request->set_body_params( $params ); 155 | 156 | $response = $this->server->dispatch( $request ); 157 | $this->assertNotInstanceOf( 'WP_Error', $response ); 158 | $this->assertEquals( 200, $response->get_status() ); 159 | 160 | $data = $response->get_data(); 161 | 162 | $this->assertArrayHasKey( 'id', $data ); 163 | $this->assertNotEquals( 0, intval( $data[ 'id' ] ) ); 164 | 165 | $params = array( 166 | 'name' => 'lizards', 167 | 'id' => $data[ 'id' ] 168 | ); 169 | $exists = Pods_API()->pod_exists( $params ); 170 | $this->assertTrue( $exists ); 171 | 172 | } 173 | 174 | /** 175 | * Test that we can update a Pod, specifically change its name 176 | * 177 | * @since 0.0.1 178 | * 179 | * @covers Pods_JSON_API_Pods_API::save_pod 180 | */ 181 | public function test_save_pod_change_pod_name() { 182 | $request = new WP_JSON_Request( 'POST', '/pods/pods-api/frogs' ); 183 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 184 | $params = array( 185 | 'name' => 'dragons', 186 | ); 187 | 188 | $request->set_body_params( $params ); 189 | 190 | $response = $this->server->dispatch( $request ); 191 | $this->assertNotInstanceOf( 'WP_Error', $response ); 192 | $this->assertEquals( 200, $response->get_status() ); 193 | 194 | $data = $response->get_data(); 195 | 196 | $this->assertArrayHasKey( 'id', $data ); 197 | $this->assertNotEquals( 0, intval( $data[ 'id' ] ) ); 198 | 199 | $params = array( 200 | 'name' => 'dragons', 201 | 'id' => $data[ 'id' ] 202 | ); 203 | $exists = Pods_API()->pod_exists( $params ); 204 | 205 | $this->assertTrue( $exists ); 206 | 207 | $params = array( 208 | 'name' => 'frogs', 209 | ); 210 | $exists = Pods_API()->pod_exists( $params ); 211 | 212 | $this->assertFalse( $exists ); 213 | 214 | $fields = pods( 'dragons' )->fields(); 215 | 216 | $fields = wp_list_pluck( $fields, 'name' ); 217 | 218 | $this->assertArrayHasKey( 'text_two', $fields ); 219 | 220 | } 221 | 222 | /** 223 | * Test that we can update a Pod, specifically that we can add a new field 224 | * 225 | * @since 0.0.1 226 | * 227 | * @covers Pods_JSON_API_Pods_API::save_pod 228 | */ 229 | public function test_save_pod_add_field() { 230 | $request = new WP_JSON_Request( 'POST', '/pods/pods-api/frogs' ); 231 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 232 | $params = array( 233 | 'fields' => array( 234 | array( 235 | 'name' => 'text_two', 236 | 'type' => 'text', 237 | ) 238 | 239 | ) 240 | ); 241 | 242 | $request->set_body_params( $params ); 243 | 244 | $response = $this->server->dispatch( $request ); 245 | $this->assertNotInstanceOf( 'WP_Error', $response ); 246 | $this->assertEquals( 200, $response->get_status() ); 247 | 248 | $data = $response->get_data(); 249 | $this->assertArrayHasKey( 'id', $data ); 250 | $this->assertNotEquals( 0, intval( $data[ 'id' ] ) ); 251 | 252 | $fields = pods( 'frogs' )->fields(); 253 | 254 | $fields = wp_list_pluck( $fields, 'name' ); 255 | 256 | $this->assertArrayHasKey( 'text_two', $fields ); 257 | 258 | } 259 | 260 | 261 | /** 262 | * Test that we can delete a Pod 263 | * 264 | * @since 0.0.1 265 | * 266 | * @covers Pods_JSON_API_Pods_API::delete_pod 267 | */ 268 | public function test_delete_pod() { 269 | $request = new WP_JSON_Request( 'DELETE', '/pods/pods-api/frogs/delete' ); 270 | 271 | $response = $this->server->dispatch( $request ); 272 | $this->assertNotInstanceOf( 'WP_Error', $response ); 273 | $this->assertEquals( 200, $response->get_status() ); 274 | $data = $response->get_data(); 275 | $this->assertArrayHasKey( 'id', $data ); 276 | 277 | $params = array( 278 | 'name' => 'frogs', 279 | 'id' => $data[ 'id' ] 280 | ); 281 | $exists = Pods_API()->pod_exists( $params ); 282 | 283 | $this->assertFalse( $exists ); 284 | 285 | } 286 | 287 | /** 288 | * Test that we can duplicate a Pod 289 | * 290 | * @since 0.0.1 291 | * 292 | * @covers Pods_JSON_API_Pods_API::duplicate_pod 293 | */ 294 | public function test_duplicate_pod() { 295 | $request = new WP_JSON_Request( 'DELETE', '/pods/pods-api/frogs/duplicate' ); 296 | $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 297 | $params = array( 298 | 'name' => 'dinosaurs' 299 | ); 300 | 301 | $request->set_body_params( $params ); 302 | 303 | $response = $this->server->dispatch( $request ); 304 | $this->assertNotInstanceOf( 'WP_Error', $response ); 305 | $this->assertEquals( 200, $response->get_status() ); 306 | $data = $response->get_data(); 307 | $this->assertArrayHasKey( 'id', $data ); 308 | 309 | $params = array( 310 | 'name' => 'frogs', 311 | 'id' => $this->pod_one_id, 312 | ); 313 | $exists = Pods_API()->pod_exists( $params ); 314 | 315 | $this->assertTrue( $exists ); 316 | 317 | $params = array( 318 | 'name' => 'dinosaurs', 319 | 'id' => $data[ 'id' ] 320 | ); 321 | $exists = Pods_API()->pod_exists( $params ); 322 | 323 | $this->assertTrue( $exists ); 324 | } 325 | 326 | /** 327 | * Test that we can get reset a Pod content. 328 | * 329 | * Note: Pods can only reset custom table Pods. Don't try and test non-existent functionality here. 330 | * 331 | * @since 0.0.1 332 | * 333 | * @covers Pods_JSON_API_Pods_API::reset_pod 334 | */ 335 | public function test_reset_pod() { 336 | $data = array( 337 | 'name' => 'foo', 338 | 'test_text' => 'bar' 339 | ); 340 | $frogs = pods( 'frogs' ); 341 | $item_id = $frogs->save( $data ); 342 | $request = new WP_JSON_Request( 'DELETE', '/pods/pods-api/frogs/reset' ); 343 | $response = $this->server->dispatch( $request ); 344 | $this->assertNotInstanceOf( 'WP_Error', $response ); 345 | $this->assertEquals( 200, $response->get_status() ); 346 | $data = $response->get_data(); 347 | $this->assertArrayHasKey( 'id', $data ); 348 | 349 | $frogs = pods( 'frogs', $item_id ); 350 | 351 | $this->assertNotEquals( $frogs->id, $item_id ); 352 | $this->assertEquals( 0, $frogs->total() ); 353 | 354 | } 355 | } 356 | -------------------------------------------------------------------------------- /tests/tests-pods-rest-api.php: -------------------------------------------------------------------------------- 1 | pods_test_case = $pods_test_case = new Pods_Unit_Tests\Pods_UnitTestCase(); 26 | 27 | activate_plugin( 'pods-rest-api/pods-rest-api.php' ); 28 | activate_plugin( 'pods/init.php' ); 29 | activate_plugin( 'json-rest-api/plugin.php' ); 30 | 31 | $pod_name = 'frogs'; 32 | $params = array( 33 | 'storage' => 'meta', 34 | 'type' => 'post_type', 35 | 'name' => $pod_name, 36 | ); 37 | 38 | $this->$pod_id = pods_api()->save_pod( $params ); 39 | } 40 | 41 | /** 42 | * Tear down 43 | * 44 | * @since 0.1.0 45 | */ 46 | function tearDown() { 47 | parent::tearDown(); 48 | } 49 | 50 | public function test_that_tests_work() { 51 | $this->assertEquals( 1, 1); 52 | } 53 | 54 | public function test_plugin_active() { 55 | $this->assertTrue( is_plugin_active( 'pods-rest-api/pods-rest-api.php' ) ); 56 | } 57 | 58 | public function test_pods_active() { 59 | $this->assertTrue( is_plugin_active( 'pods/init.php' ) ); 60 | } 61 | 62 | public function test_core_api_active() { 63 | $this->assertTrue( is_plugin_active( 'json-rest-api/plugin.php' ) ); 64 | } 65 | 66 | /** 67 | * Test that default Pods Routes exist 68 | * 69 | * @since 0.0.1 70 | * 71 | * @covers Pods_REST_API::default_routes 72 | */ 73 | public function test_pods_routes_exists() { 74 | $routes = $this->server->get_routes(); 75 | $this->assertArrayHasKey( '/pods/pods/frogs', $routes ); 76 | $this->assertArrayHasKey( '/pods/pods/frogs(?P[\d]+)', $routes ); 77 | 78 | } 79 | /** 80 | * Test that default PodsAPI Routes exist 81 | * 82 | * @since 0.0.1 83 | * 84 | * @covers Pods_REST_API::default_routes 85 | */ 86 | public function test_podsapi_routes_exists() { 87 | $routes = $this->server->get_routes(); 88 | $this->assertArrayHasKey( '/pods/podsapi/', $routes ); 89 | 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /tests/tests-request-params.php: -------------------------------------------------------------------------------- 1 | params = array( 19 | 'where' => array( 20 | 'key' => 'foo', 21 | 'value' => 'bar', 22 | 'comparison' => 'equals' 23 | ), 24 | 'orderby' => array( 25 | 'field' => 'text_test.metavalue', 26 | 'direction' => 'ASC', 27 | ), 28 | 'page' => 5, 29 | 'limit' => 11, 30 | ); 31 | 32 | $this->add_params = array( 33 | 'hats' => 'bats' 34 | ); 35 | 36 | } 37 | 38 | /** 39 | * Tear down 40 | * 41 | * @since 0.1.0 42 | */ 43 | function tearDown() { 44 | parent::tearDown(); 45 | } 46 | 47 | /** 48 | * Test we can build where params 49 | * 50 | * @since 0.0.1 51 | * 52 | * @covers Pods_REST_API_Request_Params::where 53 | */ 54 | public function test_where() { 55 | $data = Pods_REST_API_Request_Params::where( $this->params[ 'where' ] ); 56 | $this->assertSame( $this->params[ 'where' ], $data ); 57 | 58 | $data_plus_hats = Pods_REST_API_Request_Params::where( $this->params[ 'where' ], array( 'hats', 'bats' ) ); 59 | $should_be = $this->params[ 'where' ]; 60 | $should_be[ 'hats' ] = 'bats'; 61 | $this->assertSame( $data_plus_hats, $should_be ); 62 | 63 | } 64 | 65 | /** 66 | * Test we can build orderby params 67 | * 68 | * @since 0.0.1 69 | * 70 | * @covers Pods_REST_API_Request_Params::orderby 71 | */ 72 | public function test_orderby() { 73 | $data = Pods_REST_API_Request_Params::orderby( $this->params[ 'orderby' ] ); 74 | $this->assertSame( $this->params[ 'orderby' ], $data ); 75 | 76 | $data_plus_hats = Pods_REST_API_Request_Params::where( $this->params[ 'orderby' ], array( 'hats', 'bats' ) ); 77 | $should_be = $this->params[ 'orderby' ]; 78 | $should_be[ 'hats' ] = 'bats'; 79 | $this->assertSame( $data_plus_hats, $should_be ); 80 | 81 | } 82 | 83 | /** 84 | * Test we can build limit params 85 | * 86 | * @since 0.0.1 87 | * 88 | * @covers Pods_REST_API_Request_Params::limit 89 | */ 90 | public function test_limit() { 91 | $data = Pods_REST_API_Request_Params::limit( $this->params[ 'limit' ] ); 92 | $this->assertSame( $this->params[ 'limit' ], $data ); 93 | 94 | $data_plus_hats = Pods_REST_API_Request_Params::limit( $this->params[ 'limit' ], array( 'hats', 'bats' ) ); 95 | $should_be = $this->params[ 'limit' ]; 96 | $should_be[ 'hats' ] = 'bats'; 97 | $this->assertSame( $data_plus_hats, $should_be ); 98 | 99 | } 100 | 101 | /** 102 | * Test we can build page params 103 | * 104 | * @since 0.0.1 105 | * 106 | * @covers Pods_REST_API_Request_Params::page 107 | */ 108 | public function test_page() { 109 | $data = Pods_REST_API_Request_Params::page( $this->params[ 'page' ] ); 110 | $this->assertSame( $this->params[ 'page' ], $data ); 111 | 112 | $data_plus_hats = Pods_REST_API_Request_Params::page( $this->params[ 'page' ], array( 'hats', 'bats' ) ); 113 | $should_be = $this->params[ 'page' ]; 114 | $should_be[ 'hats' ] = 'bats'; 115 | $this->assertSame( $data_plus_hats, $should_be ); 116 | 117 | } 118 | 119 | /** 120 | * Test we can build full params 121 | * 122 | * @since 0.0.1 123 | * 124 | * @covers Pods_REST_API_Request_Params::page 125 | */ 126 | public function build_params() { 127 | $data = Pods_REST_API_Request_Params::build_params( $this->params[ 'page' ], 'frogs', 'GET' ); 128 | $this->assertSame( $this->params, $data ); 129 | 130 | } 131 | 132 | 133 | 134 | 135 | 136 | 137 | } 138 | --------------------------------------------------------------------------------