ACFtoWPAPI

Plugin Name: ACF to WP API Description: Puts all ACF fields from posts, pages, custom post types, attachments and taxonomy terms, into the WP-API output under the 'acf' key Author: Chris Hutchinson Author URI: http://www.chrishutchinson.me Version: 1.4.0 Plugin URI: https://wordpress.org/plugins/acf-to-wp-api/.

Defined (1)

The class is defined in the following location(s).

/acf-to-wp-api.php  
  1. class ACFtoWPAPI { 
  2.  
  3. /** 
  4. * @var object $plugin All base plugin configuration is stored here 
  5. */ 
  6. protected $plugin; 
  7.  
  8. /** 
  9. * @var string $apiVersion Stores the version number of the REST API 
  10. */ 
  11. protected $apiVersion; 
  12.  
  13. /** 
  14. * Constructor 
  15. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  16. * @since 1.4.0 Improved API version checking 
  17. * @since 1.3.3 Compatibility fix for V2.0Beta9 
  18. * @since 1.3.0 Updated to support version 2 of the WP-API 
  19. * @since 1.0.0 
  20. */ 
  21. function __construct() { 
  22. // Setup defaults 
  23. $this->plugin = new StdClass; 
  24. $this->plugin->title = 'ACF to WP API'; 
  25. $this->plugin->name = 'acf-to-wp-api'; 
  26. $this->plugin->folder = WP_PLUGIN_DIR . '/' . $this->plugin->name; 
  27. $this->plugin->url = WP_PLUGIN_URL . '/' . str_replace(basename( __FILE__), "", plugin_basename(__FILE__)); 
  28. $this->plugin->version = '1.4.0'; 
  29.  
  30. $this->apiVersion = $this->_getAPIVersion(); 
  31.  
  32. // Version One 
  33. if($this->_isAPIVersionOne()) { 
  34. $this->_versionOneSetup(); 
  35.  
  36. // Version Two 
  37. if($this->_isAPIVersionTwo()) { 
  38. $this->_versionTwoSetup();  
  39. /** 
  40. * Die and dump 
  41. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  42. * @param mixed $data The data to be dumped to the screen 
  43. *  
  44. * @return void 
  45. * @since 1.3.0 
  46. */ 
  47. private function dd($data) { 
  48. if( WP_DEBUG ) { 
  49. echo '<pre>'; 
  50. print_r($data); 
  51. echo '</pre>'; 
  52. die(); 
  53.  
  54. /** 
  55. * Adds the required filters and hooks for version 1 of the REST API 
  56. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  57. * @return void 
  58. * @since 1.3.0 
  59. */ 
  60. private function _versionOneSetup() { 
  61. // Filters 
  62. add_filter( 'json_prepare_post', array( $this, 'addACFDataPost'), 10, 3 ); // Posts 
  63. add_filter( 'json_prepare_term', array( $this, 'addACFDataTerm'), 10, 3 ); // Taxonomy Terms 
  64. add_filter( 'json_prepare_user', array( $this, 'addACFDataUser'), 10, 3 ); // Users 
  65. add_filter( 'json_prepare_comment', array( $this, 'addACFDataComment'), 10, 3 ); // Comments 
  66.  
  67. // Endpoints 
  68. add_filter( 'json_endpoints', array( $this, 'registerRoutes' ), 10, 3 ); 
  69.  
  70. /** 
  71. * Adds the required filters and hooks for version 2 of the REST API 
  72. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  73. * @return void 
  74. * @since 1.3.0 
  75. */ 
  76. private function _versionTwoSetup() { 
  77. // Actions 
  78. add_action( 'rest_api_init', array( $this, 'addACFDataPostV2' ) ); // Posts 
  79. add_action( 'rest_api_init', array( $this, 'addACFDataTermV2' ) ); // Taxonomy Terms 
  80. add_action( 'rest_api_init', array( $this, 'addACFDataUserV2' ) ); // Users 
  81. add_action( 'rest_api_init', array( $this, 'addACFDataCommentV2' ) ); // Comments 
  82.  
  83. add_action( 'rest_api_init', array( $this, 'addACFOptionRouteV2') ); 
  84.  
  85. /** 
  86. * Returns the WP REST API version, assumes version 2 
  87. * if can't find any other version 
  88. *  
  89. * @return string The version number, set by WP REST API 
  90. *  
  91. * @since 1.3.2 
  92. */ 
  93. private function _getAPIVersion() { 
  94. $version = 2; 
  95.  
  96. if ( defined('REST_API_VERSION') ) { 
  97. $version = REST_API_VERSION; 
  98. } else { 
  99. $version = get_option( 'rest_api_plugin_version', get_option( 'json_api_plugin_version', null ) ); 
  100.  
  101. return $version; 
  102.  
  103. /** 
  104. * Gets the version number of the WP REST API 
  105. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  106. * @return int The base version number 
  107. * @since 1.3.0 
  108. */ 
  109. private function _getAPIBaseVersion() { 
  110. $version = $this->apiVersion; 
  111.  
  112. if( is_null( $version ) ) { 
  113. return false; 
  114.  
  115. $baseNumber = (int) substr( $version, 0, 1 ); 
  116.  
  117. if( $baseNumber > 0 ) { 
  118. return $baseNumber; 
  119.  
  120. return false; 
  121.  
  122. /** 
  123. * Check if the current API base version is version 1 
  124. * @return boolean True if the current API version is 1 
  125. * @since 1.3.0 
  126. */ 
  127. private function _isAPIVersionOne() { 
  128. if($this->_getAPIBaseVersion() === 1) {  
  129. return true; 
  130.  
  131. return false; 
  132.  
  133. /** 
  134. * Check if the current API base version is version 2 
  135. * @return boolean True if the current API version is 2 
  136. * @since 1.3.0 
  137. */ 
  138. private function _isAPIVersionTwo() { 
  139. if($this->_getAPIBaseVersion() === 2) {  
  140. return true; 
  141.  
  142. return false; 
  143.  
  144. /** 
  145. * Add data to users 
  146. * @param array $data The current ACF data 
  147. * @param int $user The ID of the user 
  148. * @param string $context The context the data is being requested in 
  149. * @since 1.1.0 
  150. */ 
  151. function addACFDataUser( $data, $user, $context ) { 
  152. $data['acf'] = $this->_getData( $user->ID, 'user' ); 
  153. return $data; 
  154.  
  155. /** 
  156. * Add data to terms 
  157. * @param array $data The current ACF data 
  158. * @param int $term The ID of the term 
  159. * @param string $context The context the data is being requested in 
  160. * @since 1.1.0 
  161. */ 
  162. function addACFDataTerm( $data, $term, $context = null ) { 
  163. $data['acf'] = get_fields( $term, 'term' ); 
  164. return $data; 
  165.  
  166. /** 
  167. * Add data to Posts, Custom Post Types, Pages & Attachments 
  168. * @param array $data The current ACF data 
  169. * @param int $post The ID of the record 
  170. * @param string $context The context the data is being requested in 
  171. * @since 1.1.0 
  172. */ 
  173. function addACFDataPost( $data, $post, $context ) { 
  174. $data['acf'] = $this->_getData( $post['ID'] ); 
  175. return $data; 
  176.  
  177. /** 
  178. * Registers the `acf` field against posts 
  179. * @return void 
  180. * @since 1.3.2 Adds support for pages and public custom post types 
  181. * @since 1.3.0 
  182. */ 
  183. function addACFDataPostV2() { 
  184. // Posts 
  185. register_rest_field( 'post',  
  186. 'acf',  
  187. array( 
  188. 'get_callback' => array( $this, 'addACFDataPostV2cb' ),  
  189. 'update_callback' => null,  
  190. 'schema' => null,  
  191. ); 
  192.  
  193. // Pages 
  194. register_rest_field( 'page',  
  195. 'acf',  
  196. array( 
  197. 'get_callback' => array( $this, 'addACFDataPostV2cb' ),  
  198. 'update_callback' => null,  
  199. 'schema' => null,  
  200. ); 
  201.  
  202. // Public custom post types 
  203. $types = get_post_types(array( 
  204. 'public' => true,  
  205. '_builtin' => false 
  206. )); 
  207. foreach($types as $key => $type) { 
  208. register_rest_field( $type,  
  209. 'acf',  
  210. array( 
  211. 'get_callback' => array( $this, 'addACFDataPostV2cb' ),  
  212. 'update_callback' => null,  
  213. 'schema' => null,  
  214. ); 
  215.  
  216. /** 
  217. * Returns the ACF data to be added to the JSON response posts 
  218. *  
  219. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  220. * @param array $object The object to get data for 
  221. * @param string $fieldName The name of the field being completed 
  222. * @param object $request The WP_REST_REQUEST object 
  223. *  
  224. * @return array The data for this object type 
  225. * @see ACFtoWPAPI::addACFDataPostV2() 
  226. * @since 1.3.0 
  227. */ 
  228. function addACFDataPostV2cb($object, $fieldName, $request) { 
  229. return $this->_getData($object['id']); 
  230.  
  231. /** 
  232. * Registers the `acf` field against taxonomy terms 
  233. * @return void 
  234. * @since 1.3.0 
  235. */ 
  236. function addACFDataTermV2() { 
  237. register_rest_field( 'term',  
  238. 'acf',  
  239. array( 
  240. 'get_callback' => array( $this, 'addACFDataTermV2cb' ),  
  241. 'update_callback' => null,  
  242. 'schema' => null,  
  243. ); 
  244.  
  245. /** 
  246. * Returns the ACF data to be added to the JSON response for taxonomy terms 
  247. *  
  248. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  249. * @param array $object The object to get data for 
  250. * @param string $fieldName The name of the field being completed 
  251. * @param object $request The WP_REST_REQUEST object 
  252. *  
  253. * @return array The data for this object type 
  254. * @see ACFtoWPAPI::addACFDataTermV2() 
  255. * @since 1.3.0 
  256. */ 
  257. function addACFDataTermV2cb($object, $fieldName, $request) { 
  258. return $this->_getData($object['id'], 'term', $object); 
  259.  
  260. /** 
  261. * Registers the `acf` field against users 
  262. * @return void 
  263. * @since 1.3.0 
  264. */ 
  265. function addACFDataUserV2() { 
  266. register_rest_field( 'user',  
  267. 'acf',  
  268. array( 
  269. 'get_callback' => array( $this, 'addACFDataUserV2cb' ),  
  270. 'update_callback' => null,  
  271. 'schema' => null,  
  272. ); 
  273.  
  274. /** 
  275. * Returns the ACF data to be added to the JSON response for users 
  276. *  
  277. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  278. * @param array $object The object to get data for 
  279. * @param string $fieldName The name of the field being completed 
  280. * @param object $request The WP_REST_REQUEST object 
  281. *  
  282. * @return array The data for this object type 
  283. * @see ACFtoWPAPI::addACFDataUserV2() 
  284. * @since 1.3.0 
  285. */ 
  286. function addACFDataUserV2cb($object, $fieldName, $request) { 
  287. return $this->_getData( $object['id'], 'user' ); 
  288.  
  289. /** 
  290. * Registers the `acf` field against comments 
  291. * @return void 
  292. * @since 1.3.0 
  293. */ 
  294. function addACFDataCommentV2() { 
  295. register_rest_field( 'comment',  
  296. 'acf',  
  297. array( 
  298. 'get_callback' => array( $this, 'addACFDataCommentV2cb' ),  
  299. 'update_callback' => null,  
  300. 'schema' => null,  
  301. ); 
  302.  
  303. /** 
  304. * Returns the ACF data to be added to the JSON response for comments 
  305. *  
  306. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  307. * @param array $object The object to get data for 
  308. * @param string $fieldName The name of the field being completed 
  309. * @param object $request The WP_REST_REQUEST object 
  310. *  
  311. * @return array The data for this object type 
  312. * @see ACFtoWPAPI::addACFDataCommentV2() 
  313. * @since 1.3.0 
  314. */ 
  315. function addACFDataCommentV2cb( $object, $fieldName, $request ) { 
  316. return $this->_getData( $object['id'], 'comment' ); 
  317.  
  318. /** 
  319. * Returns an array of Advanced Custom Fields data for the given record 
  320. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  321. *  
  322. * @param int $id The ID of the object to get 
  323. * @param string $type The type of the object to get 
  324. * @param array $object The full object being requested, only required for specific $types 
  325. * @return array The Advanced Custom Fields data for the supplied record 
  326. *  
  327. * @since 1.3.0 
  328. */ 
  329. private function _getData($id, $type = 'post', $object = array()) { 
  330. switch($type) { 
  331. case 'post': 
  332. default: 
  333. return get_fields($id); 
  334. break; 
  335. case 'term': 
  336. return get_fields($object['taxonomy'] . '_' . $id); 
  337. break; 
  338. case 'user': 
  339. return get_fields('user_' . $id); 
  340. break; 
  341. case 'comment': 
  342. return get_fields('comment_' . $id); 
  343. break; 
  344. case 'options': 
  345. return get_fields('option'); 
  346. break; 
  347.  
  348. /** 
  349. * Registers the routes for all and single options 
  350. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  351. * @return void 
  352. * @since 1.3.1 Switched to array() notation (over [] notation) to support PHP < 5.4 
  353. * @since 1.3.0 
  354. */ 
  355. function addACFOptionRouteV2() { 
  356. register_rest_route( 'wp/v2/acf', '/options', array( 
  357. 'methods' => array( 
  358. 'GET' 
  359. ),  
  360. 'callback' => array( $this, 'addACFOptionRouteV2cb' ) 
  361. ) ); 
  362.  
  363. register_rest_route( 'wp/v2/acf', '/options/(?P<option>.+)', array( 
  364. 'methods' => array( 
  365. 'GET' 
  366. ),  
  367. 'callback' => array( $this, 'addACFOptionRouteV2cb' ) 
  368. ) ); 
  369.  
  370. /** 
  371. * The callback for the `wp/v2/acf/options` endpoint 
  372. *  
  373. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  374. * @param WP_REST_Request $request The WP_REST_Request object 
  375. * @return array|string The single requested option, or all options  
  376. * @see ACFtoWPAPI::addACFOptionRouteV2() 
  377. * @since 1.3.0 
  378. */ 
  379. function addACFOptionRouteV2cb( WP_REST_Request $request ) { 
  380. if($request['option']) { 
  381. return get_field($request['option'], 'option'); 
  382.  
  383. return get_fields('option'); 
  384.  
  385. /** 
  386. * Returns data for comments (WP API v1) 
  387. * @author Chris Hutchinson <chris_hutchinson@me.com> 
  388. * @param array $data The response data to be extended 
  389. * @param object $comment The comment being requested 
  390. * @param string $context The context the data is being requested in 
  391. * @return array The extended $data array, with ACF data 
  392. * @since 1.1.0 
  393. */ 
  394. function addACFDataComment($data, $comment, $context) { 
  395. $data['acf'] = $this->_getData('comment_' . $comment->comment_ID); 
  396. return $data; 
  397.  
  398. /** 
  399. * Returns data for options (WP API v1) 
  400. * @author github.com/kokarn 
  401. * @return array The options data 
  402. * @since 1.1.0 
  403. */ 
  404. function getACFOptions() { 
  405. return get_fields('options'); 
  406.  
  407. /** 
  408. * Returns a single option based on the supplied name (WP API v1) 
  409. * @author github.com/asquel 
  410. * @param string $name The option name being requested 
  411. * @return mixed The data for the supplied option  
  412. * @since 1.3.0 
  413. */ 
  414. function getACFOption($name) { 
  415. return get_field($name, 'option'); 
  416.  
  417. /** 
  418. * Registers additional routes (WP API v1) 
  419. * @author github.com/kokarn 
  420. * @return array The routes data 
  421. * @since 1.1.0 
  422. */ 
  423. function registerRoutes( $routes ) { 
  424. $routes['/option'] = array( 
  425. array( array( $this, 'getACFOptions' ), WP_JSON_Server::READABLE ) 
  426. ); 
  427. $routes['/options'] = array( 
  428. array( array( $this, 'getACFOptions' ), WP_JSON_Server::READABLE ) 
  429. ); 
  430.  
  431. $routes['/options/(?P<name>[\w-]+)'] = array( 
  432. array( array( $this, 'getACFOption' ), WP_JSON_Server::READABLE ),  
  433. ); 
  434.  
  435. return $routes; 
  436.