acf.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. /**
  3. * Advanced Custom Fields PRO
  4. *
  5. * @package ACF
  6. * @author WP Engine
  7. *
  8. * @wordpress-plugin
  9. * Plugin Name: Advanced Custom Fields PRO
  10. * Plugin URI: https://www.advancedcustomfields.com
  11. * Description: Customize WordPress with powerful, professional and intuitive fields.
  12. * Version: 6.0.6
  13. * Author: WP Engine
  14. * Author URI: https://wpengine.com/?utm_source=wordpress.org&utm_medium=referral&utm_campaign=plugin_directory&utm_content=advanced_custom_fields
  15. * Update URI: https://www.advancedcustomfields.com/pro
  16. * Text Domain: acf
  17. * Domain Path: /lang
  18. */
  19. if ( ! defined( 'ABSPATH' ) ) {
  20. exit; // Exit if accessed directly.
  21. }
  22. if ( ! class_exists( 'ACF' ) ) {
  23. /**
  24. * The main ACF class
  25. */
  26. class ACF {
  27. /**
  28. * The plugin version number.
  29. *
  30. * @var string
  31. */
  32. public $version = '6.0.6';
  33. /**
  34. * The plugin settings array.
  35. *
  36. * @var array
  37. */
  38. public $settings = array();
  39. /**
  40. * The plugin data array.
  41. *
  42. * @var array
  43. */
  44. public $data = array();
  45. /**
  46. * Storage for class instances.
  47. *
  48. * @var array
  49. */
  50. public $instances = array();
  51. /**
  52. * A dummy constructor to ensure ACF is only setup once.
  53. *
  54. * @date 23/06/12
  55. * @since 5.0.0
  56. *
  57. * @return void
  58. */
  59. public function __construct() {
  60. // Do nothing.
  61. }
  62. /**
  63. * Sets up the ACF plugin.
  64. *
  65. * @date 28/09/13
  66. * @since 5.0.0
  67. *
  68. * @return void
  69. */
  70. public function initialize() {
  71. // Define constants.
  72. $this->define( 'ACF', true );
  73. $this->define( 'ACF_PATH', plugin_dir_path( __FILE__ ) );
  74. $this->define( 'ACF_BASENAME', plugin_basename( __FILE__ ) );
  75. $this->define( 'ACF_VERSION', $this->version );
  76. $this->define( 'ACF_MAJOR_VERSION', 6 );
  77. $this->define( 'ACF_FIELD_API_VERSION', 5 );
  78. $this->define( 'ACF_UPGRADE_VERSION', '5.5.0' ); // Highest version with an upgrade routine. See upgrades.php.
  79. // Define settings.
  80. $this->settings = array(
  81. 'name' => __( 'Advanced Custom Fields', 'acf' ),
  82. 'slug' => dirname( ACF_BASENAME ),
  83. 'version' => ACF_VERSION,
  84. 'basename' => ACF_BASENAME,
  85. 'path' => ACF_PATH,
  86. 'file' => __FILE__,
  87. 'url' => plugin_dir_url( __FILE__ ),
  88. 'show_admin' => true,
  89. 'show_updates' => true,
  90. 'stripslashes' => false,
  91. 'local' => true,
  92. 'json' => true,
  93. 'save_json' => '',
  94. 'load_json' => array(),
  95. 'default_language' => '',
  96. 'current_language' => '',
  97. 'capability' => 'manage_options',
  98. 'uploader' => 'wp',
  99. 'autoload' => false,
  100. 'l10n' => true,
  101. 'l10n_textdomain' => '',
  102. 'google_api_key' => '',
  103. 'google_api_client' => '',
  104. 'enqueue_google_maps' => true,
  105. 'enqueue_select2' => true,
  106. 'enqueue_datepicker' => true,
  107. 'enqueue_datetimepicker' => true,
  108. 'select2_version' => 4,
  109. 'row_index_offset' => 1,
  110. 'remove_wp_meta_box' => true,
  111. 'rest_api_enabled' => true,
  112. 'rest_api_format' => 'light',
  113. 'rest_api_embed_links' => true,
  114. 'preload_blocks' => true,
  115. 'enable_shortcode' => true,
  116. );
  117. // Include utility functions.
  118. include_once ACF_PATH . 'includes/acf-utility-functions.php';
  119. // Include previous API functions.
  120. acf_include( 'includes/api/api-helpers.php' );
  121. acf_include( 'includes/api/api-template.php' );
  122. acf_include( 'includes/api/api-term.php' );
  123. // Include classes.
  124. acf_include( 'includes/class-acf-data.php' );
  125. acf_include( 'includes/fields/class-acf-field.php' );
  126. acf_include( 'includes/locations/abstract-acf-legacy-location.php' );
  127. acf_include( 'includes/locations/abstract-acf-location.php' );
  128. // Include functions.
  129. acf_include( 'includes/acf-helper-functions.php' );
  130. acf_include( 'includes/acf-hook-functions.php' );
  131. acf_include( 'includes/acf-field-functions.php' );
  132. acf_include( 'includes/acf-field-group-functions.php' );
  133. acf_include( 'includes/acf-form-functions.php' );
  134. acf_include( 'includes/acf-meta-functions.php' );
  135. acf_include( 'includes/acf-post-functions.php' );
  136. acf_include( 'includes/acf-user-functions.php' );
  137. acf_include( 'includes/acf-value-functions.php' );
  138. acf_include( 'includes/acf-input-functions.php' );
  139. acf_include( 'includes/acf-wp-functions.php' );
  140. // Include core.
  141. acf_include( 'includes/fields.php' );
  142. acf_include( 'includes/locations.php' );
  143. acf_include( 'includes/assets.php' );
  144. acf_include( 'includes/compatibility.php' );
  145. acf_include( 'includes/deprecated.php' );
  146. acf_include( 'includes/l10n.php' );
  147. acf_include( 'includes/local-fields.php' );
  148. acf_include( 'includes/local-meta.php' );
  149. acf_include( 'includes/local-json.php' );
  150. acf_include( 'includes/loop.php' );
  151. acf_include( 'includes/media.php' );
  152. acf_include( 'includes/revisions.php' );
  153. acf_include( 'includes/updates.php' );
  154. acf_include( 'includes/upgrades.php' );
  155. acf_include( 'includes/validation.php' );
  156. acf_include( 'includes/rest-api.php' );
  157. // Include ajax.
  158. acf_include( 'includes/ajax/class-acf-ajax.php' );
  159. acf_include( 'includes/ajax/class-acf-ajax-check-screen.php' );
  160. acf_include( 'includes/ajax/class-acf-ajax-user-setting.php' );
  161. acf_include( 'includes/ajax/class-acf-ajax-upgrade.php' );
  162. acf_include( 'includes/ajax/class-acf-ajax-query.php' );
  163. acf_include( 'includes/ajax/class-acf-ajax-query-users.php' );
  164. acf_include( 'includes/ajax/class-acf-ajax-local-json-diff.php' );
  165. // Include forms.
  166. acf_include( 'includes/forms/form-attachment.php' );
  167. acf_include( 'includes/forms/form-comment.php' );
  168. acf_include( 'includes/forms/form-customizer.php' );
  169. acf_include( 'includes/forms/form-front.php' );
  170. acf_include( 'includes/forms/form-nav-menu.php' );
  171. acf_include( 'includes/forms/form-post.php' );
  172. acf_include( 'includes/forms/form-gutenberg.php' );
  173. acf_include( 'includes/forms/form-taxonomy.php' );
  174. acf_include( 'includes/forms/form-user.php' );
  175. acf_include( 'includes/forms/form-widget.php' );
  176. // Include admin.
  177. if ( is_admin() ) {
  178. acf_include( 'includes/admin/admin.php' );
  179. acf_include( 'includes/admin/admin-field-group.php' );
  180. acf_include( 'includes/admin/admin-field-groups.php' );
  181. acf_include( 'includes/admin/admin-notices.php' );
  182. acf_include( 'includes/admin/admin-tools.php' );
  183. acf_include( 'includes/admin/admin-upgrade.php' );
  184. }
  185. // Include legacy.
  186. acf_include( 'includes/legacy/legacy-locations.php' );
  187. // Include PRO.
  188. acf_include( 'pro/acf-pro.php' );
  189. // Add actions.
  190. add_action( 'init', array( $this, 'init' ), 5 );
  191. add_action( 'init', array( $this, 'register_post_types' ), 5 );
  192. add_action( 'init', array( $this, 'register_post_status' ), 5 );
  193. add_action( 'activated_plugin', array( $this, 'deactivate_other_instances' ) );
  194. add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) );
  195. // Add filters.
  196. add_filter( 'posts_where', array( $this, 'posts_where' ), 10, 2 );
  197. }
  198. /**
  199. * Completes the setup process on "init" of earlier.
  200. *
  201. * @date 28/09/13
  202. * @since 5.0.0
  203. *
  204. * @return void
  205. */
  206. public function init() {
  207. // Bail early if called directly from functions.php or plugin file.
  208. if ( ! did_action( 'plugins_loaded' ) ) {
  209. return;
  210. }
  211. // This function may be called directly from template functions. Bail early if already did this.
  212. if ( acf_did( 'init' ) ) {
  213. return;
  214. }
  215. // Update url setting. Allows other plugins to modify the URL (force SSL).
  216. acf_update_setting( 'url', plugin_dir_url( __FILE__ ) );
  217. // Load textdomain file.
  218. acf_load_textdomain();
  219. // Include 3rd party compatiblity.
  220. acf_include( 'includes/third-party.php' );
  221. // Include wpml support.
  222. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
  223. acf_include( 'includes/wpml.php' );
  224. }
  225. // Include fields.
  226. acf_include( 'includes/fields/class-acf-field-text.php' );
  227. acf_include( 'includes/fields/class-acf-field-textarea.php' );
  228. acf_include( 'includes/fields/class-acf-field-number.php' );
  229. acf_include( 'includes/fields/class-acf-field-range.php' );
  230. acf_include( 'includes/fields/class-acf-field-email.php' );
  231. acf_include( 'includes/fields/class-acf-field-url.php' );
  232. acf_include( 'includes/fields/class-acf-field-password.php' );
  233. acf_include( 'includes/fields/class-acf-field-image.php' );
  234. acf_include( 'includes/fields/class-acf-field-file.php' );
  235. acf_include( 'includes/fields/class-acf-field-wysiwyg.php' );
  236. acf_include( 'includes/fields/class-acf-field-oembed.php' );
  237. acf_include( 'includes/fields/class-acf-field-select.php' );
  238. acf_include( 'includes/fields/class-acf-field-checkbox.php' );
  239. acf_include( 'includes/fields/class-acf-field-radio.php' );
  240. acf_include( 'includes/fields/class-acf-field-button-group.php' );
  241. acf_include( 'includes/fields/class-acf-field-true_false.php' );
  242. acf_include( 'includes/fields/class-acf-field-link.php' );
  243. acf_include( 'includes/fields/class-acf-field-post_object.php' );
  244. acf_include( 'includes/fields/class-acf-field-page_link.php' );
  245. acf_include( 'includes/fields/class-acf-field-relationship.php' );
  246. acf_include( 'includes/fields/class-acf-field-taxonomy.php' );
  247. acf_include( 'includes/fields/class-acf-field-user.php' );
  248. acf_include( 'includes/fields/class-acf-field-google-map.php' );
  249. acf_include( 'includes/fields/class-acf-field-date_picker.php' );
  250. acf_include( 'includes/fields/class-acf-field-date_time_picker.php' );
  251. acf_include( 'includes/fields/class-acf-field-time_picker.php' );
  252. acf_include( 'includes/fields/class-acf-field-color_picker.php' );
  253. acf_include( 'includes/fields/class-acf-field-message.php' );
  254. acf_include( 'includes/fields/class-acf-field-accordion.php' );
  255. acf_include( 'includes/fields/class-acf-field-tab.php' );
  256. acf_include( 'includes/fields/class-acf-field-group.php' );
  257. /**
  258. * Fires after field types have been included.
  259. *
  260. * @date 28/09/13
  261. * @since 5.0.0
  262. *
  263. * @param int ACF_FIELD_API_VERSION The field API version.
  264. */
  265. do_action( 'acf/include_field_types', ACF_FIELD_API_VERSION );
  266. // Include locations.
  267. acf_include( 'includes/locations/class-acf-location-post-type.php' );
  268. acf_include( 'includes/locations/class-acf-location-post-template.php' );
  269. acf_include( 'includes/locations/class-acf-location-post-status.php' );
  270. acf_include( 'includes/locations/class-acf-location-post-format.php' );
  271. acf_include( 'includes/locations/class-acf-location-post-category.php' );
  272. acf_include( 'includes/locations/class-acf-location-post-taxonomy.php' );
  273. acf_include( 'includes/locations/class-acf-location-post.php' );
  274. acf_include( 'includes/locations/class-acf-location-page-template.php' );
  275. acf_include( 'includes/locations/class-acf-location-page-type.php' );
  276. acf_include( 'includes/locations/class-acf-location-page-parent.php' );
  277. acf_include( 'includes/locations/class-acf-location-page.php' );
  278. acf_include( 'includes/locations/class-acf-location-current-user.php' );
  279. acf_include( 'includes/locations/class-acf-location-current-user-role.php' );
  280. acf_include( 'includes/locations/class-acf-location-user-form.php' );
  281. acf_include( 'includes/locations/class-acf-location-user-role.php' );
  282. acf_include( 'includes/locations/class-acf-location-taxonomy.php' );
  283. acf_include( 'includes/locations/class-acf-location-attachment.php' );
  284. acf_include( 'includes/locations/class-acf-location-comment.php' );
  285. acf_include( 'includes/locations/class-acf-location-widget.php' );
  286. acf_include( 'includes/locations/class-acf-location-nav-menu.php' );
  287. acf_include( 'includes/locations/class-acf-location-nav-menu-item.php' );
  288. /**
  289. * Fires after location types have been included.
  290. *
  291. * @date 28/09/13
  292. * @since 5.0.0
  293. *
  294. * @param int ACF_FIELD_API_VERSION The field API version.
  295. */
  296. do_action( 'acf/include_location_rules', ACF_FIELD_API_VERSION );
  297. /**
  298. * Fires during initialization. Used to add local fields.
  299. *
  300. * @date 28/09/13
  301. * @since 5.0.0
  302. *
  303. * @param int ACF_FIELD_API_VERSION The field API version.
  304. */
  305. do_action( 'acf/include_fields', ACF_FIELD_API_VERSION );
  306. /**
  307. * Fires after ACF is completely "initialized".
  308. *
  309. * @date 28/09/13
  310. * @since 5.0.0
  311. *
  312. * @param int ACF_MAJOR_VERSION The major version of ACF.
  313. */
  314. do_action( 'acf/init', ACF_MAJOR_VERSION );
  315. }
  316. /**
  317. * Registers the ACF post types.
  318. *
  319. * @date 22/10/2015
  320. * @since 5.3.2
  321. *
  322. * @return void
  323. */
  324. public function register_post_types() {
  325. // Vars.
  326. $cap = acf_get_setting( 'capability' );
  327. // Register the Field Group post type.
  328. register_post_type(
  329. 'acf-field-group',
  330. array(
  331. 'labels' => array(
  332. 'name' => __( 'Field Groups', 'acf' ),
  333. 'singular_name' => __( 'Field Group', 'acf' ),
  334. 'add_new' => __( 'Add New', 'acf' ),
  335. 'add_new_item' => __( 'Add New Field Group', 'acf' ),
  336. 'edit_item' => __( 'Edit Field Group', 'acf' ),
  337. 'new_item' => __( 'New Field Group', 'acf' ),
  338. 'view_item' => __( 'View Field Group', 'acf' ),
  339. 'search_items' => __( 'Search Field Groups', 'acf' ),
  340. 'not_found' => __( 'No Field Groups found', 'acf' ),
  341. 'not_found_in_trash' => __( 'No Field Groups found in Trash', 'acf' ),
  342. ),
  343. 'public' => false,
  344. 'hierarchical' => true,
  345. 'show_ui' => true,
  346. 'show_in_menu' => false,
  347. '_builtin' => false,
  348. 'capability_type' => 'post',
  349. 'capabilities' => array(
  350. 'edit_post' => $cap,
  351. 'delete_post' => $cap,
  352. 'edit_posts' => $cap,
  353. 'delete_posts' => $cap,
  354. ),
  355. 'supports' => false,
  356. 'rewrite' => false,
  357. 'query_var' => false,
  358. )
  359. );
  360. // Register the Field post type.
  361. register_post_type(
  362. 'acf-field',
  363. array(
  364. 'labels' => array(
  365. 'name' => __( 'Fields', 'acf' ),
  366. 'singular_name' => __( 'Field', 'acf' ),
  367. 'add_new' => __( 'Add New', 'acf' ),
  368. 'add_new_item' => __( 'Add New Field', 'acf' ),
  369. 'edit_item' => __( 'Edit Field', 'acf' ),
  370. 'new_item' => __( 'New Field', 'acf' ),
  371. 'view_item' => __( 'View Field', 'acf' ),
  372. 'search_items' => __( 'Search Fields', 'acf' ),
  373. 'not_found' => __( 'No Fields found', 'acf' ),
  374. 'not_found_in_trash' => __( 'No Fields found in Trash', 'acf' ),
  375. ),
  376. 'public' => false,
  377. 'hierarchical' => true,
  378. 'show_ui' => false,
  379. 'show_in_menu' => false,
  380. '_builtin' => false,
  381. 'capability_type' => 'post',
  382. 'capabilities' => array(
  383. 'edit_post' => $cap,
  384. 'delete_post' => $cap,
  385. 'edit_posts' => $cap,
  386. 'delete_posts' => $cap,
  387. ),
  388. 'supports' => array( 'title' ),
  389. 'rewrite' => false,
  390. 'query_var' => false,
  391. )
  392. );
  393. }
  394. /**
  395. * Registers the ACF post statuses.
  396. *
  397. * @date 22/10/2015
  398. * @since 5.3.2
  399. *
  400. * @return void
  401. */
  402. public function register_post_status() {
  403. // Register the Inactive post status.
  404. register_post_status(
  405. 'acf-disabled',
  406. array(
  407. 'label' => _x( 'Inactive', 'post status', 'acf' ),
  408. 'public' => true,
  409. 'exclude_from_search' => false,
  410. 'show_in_admin_all_list' => true,
  411. 'show_in_admin_status_list' => true,
  412. /* translators: counts for inactive field groups */
  413. 'label_count' => _n_noop( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', 'acf' ),
  414. )
  415. );
  416. }
  417. /**
  418. * Checks if another version of ACF/ACF PRO is active and deactivates it.
  419. * Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated.
  420. *
  421. * @param string $plugin The plugin being activated.
  422. */
  423. public function deactivate_other_instances( $plugin ) {
  424. if ( ! in_array( $plugin, array( 'advanced-custom-fields/acf.php', 'advanced-custom-fields-pro/acf.php' ), true ) ) {
  425. return;
  426. }
  427. $plugin_to_deactivate = 'advanced-custom-fields/acf.php';
  428. $deactivated_notice_id = '1';
  429. // If we just activated the free version, deactivate the pro version.
  430. if ( $plugin === $plugin_to_deactivate ) {
  431. $plugin_to_deactivate = 'advanced-custom-fields-pro/acf.php';
  432. $deactivated_notice_id = '2';
  433. }
  434. if ( is_multisite() && is_network_admin() ) {
  435. $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  436. $active_plugins = array_keys( $active_plugins );
  437. } else {
  438. $active_plugins = (array) get_option( 'active_plugins', array() );
  439. }
  440. foreach ( $active_plugins as $plugin_basename ) {
  441. if ( $plugin_to_deactivate === $plugin_basename ) {
  442. set_transient( 'acf_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS );
  443. deactivate_plugins( $plugin_basename );
  444. return;
  445. }
  446. }
  447. }
  448. /**
  449. * Displays a notice when either ACF or ACF PRO is automatically deactivated.
  450. */
  451. public function plugin_deactivated_notice() {
  452. $deactivated_notice_id = (int) get_transient( 'acf_deactivated_notice_id' );
  453. if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) {
  454. return;
  455. }
  456. $message = __( "Advanced Custom Fields and Advanced Custom Fields PRO should not be active at the same time. We've automatically deactivated Advanced Custom Fields.", 'acf' );
  457. if ( 2 === $deactivated_notice_id ) {
  458. $message = __( "Advanced Custom Fields and Advanced Custom Fields PRO should not be active at the same time. We've automatically deactivated Advanced Custom Fields PRO.", 'acf' );
  459. }
  460. ?>
  461. <div class="updated" style="border-left: 4px solid #ffba00;">
  462. <p><?php echo esc_html( $message ); ?></p>
  463. </div>
  464. <?php
  465. delete_transient( 'acf_deactivated_notice_id' );
  466. }
  467. /**
  468. * Filters the $where clause allowing for custom WP_Query args.
  469. *
  470. * @date 31/8/19
  471. * @since 5.8.1
  472. *
  473. * @param string $where The WHERE clause.
  474. * @param WP_Query $wp_query The query object.
  475. * @return WP_Query $wp_query The query object.
  476. */
  477. public function posts_where( $where, $wp_query ) {
  478. global $wpdb;
  479. $field_key = $wp_query->get( 'acf_field_key' );
  480. $field_name = $wp_query->get( 'acf_field_name' );
  481. $group_key = $wp_query->get( 'acf_group_key' );
  482. // Add custom "acf_field_key" arg.
  483. if ( $field_key ) {
  484. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name = %s", $field_key );
  485. }
  486. // Add custom "acf_field_name" arg.
  487. if ( $field_name ) {
  488. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_excerpt = %s", $field_name );
  489. }
  490. // Add custom "acf_group_key" arg.
  491. if ( $group_key ) {
  492. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name = %s", $group_key );
  493. }
  494. // Return.
  495. return $where;
  496. }
  497. /**
  498. * Defines a constant if doesnt already exist.
  499. *
  500. * @date 3/5/17
  501. * @since 5.5.13
  502. *
  503. * @param string $name The constant name.
  504. * @param mixed $value The constant value.
  505. * @return void
  506. */
  507. public function define( $name, $value = true ) {
  508. if ( ! defined( $name ) ) {
  509. define( $name, $value );
  510. }
  511. }
  512. /**
  513. * Returns true if a setting exists for this name.
  514. *
  515. * @date 2/2/18
  516. * @since 5.6.5
  517. *
  518. * @param string $name The setting name.
  519. * @return boolean
  520. */
  521. public function has_setting( $name ) {
  522. return isset( $this->settings[ $name ] );
  523. }
  524. /**
  525. * Returns a setting or null if doesn't exist.
  526. *
  527. * @date 28/09/13
  528. * @since 5.0.0
  529. *
  530. * @param string $name The setting name.
  531. * @return mixed
  532. */
  533. public function get_setting( $name ) {
  534. return isset( $this->settings[ $name ] ) ? $this->settings[ $name ] : null;
  535. }
  536. /**
  537. * Updates a setting for the given name and value.
  538. *
  539. * @date 28/09/13
  540. * @since 5.0.0
  541. *
  542. * @param string $name The setting name.
  543. * @param mixed $value The setting value.
  544. * @return true
  545. */
  546. public function update_setting( $name, $value ) {
  547. $this->settings[ $name ] = $value;
  548. return true;
  549. }
  550. /**
  551. * Returns data or null if doesn't exist.
  552. *
  553. * @date 28/09/13
  554. * @since 5.0.0
  555. *
  556. * @param string $name The data name.
  557. * @return mixed
  558. */
  559. public function get_data( $name ) {
  560. return isset( $this->data[ $name ] ) ? $this->data[ $name ] : null;
  561. }
  562. /**
  563. * Sets data for the given name and value.
  564. *
  565. * @date 28/09/13
  566. * @since 5.0.0
  567. *
  568. * @param string $name The data name.
  569. * @param mixed $value The data value.
  570. * @return void
  571. */
  572. public function set_data( $name, $value ) {
  573. $this->data[ $name ] = $value;
  574. }
  575. /**
  576. * Returns an instance or null if doesn't exist.
  577. *
  578. * @date 13/2/18
  579. * @since 5.6.9
  580. *
  581. * @param string $class The instance class name.
  582. * @return object
  583. */
  584. public function get_instance( $class ) {
  585. $name = strtolower( $class );
  586. return isset( $this->instances[ $name ] ) ? $this->instances[ $name ] : null;
  587. }
  588. /**
  589. * Creates and stores an instance of the given class.
  590. *
  591. * @date 13/2/18
  592. * @since 5.6.9
  593. *
  594. * @param string $class The instance class name.
  595. * @return object
  596. */
  597. public function new_instance( $class ) {
  598. $instance = new $class();
  599. $name = strtolower( $class );
  600. $this->instances[ $name ] = $instance;
  601. return $instance;
  602. }
  603. /**
  604. * Magic __isset method for backwards compatibility.
  605. *
  606. * @date 24/4/20
  607. * @since 5.9.0
  608. *
  609. * @param string $key Key name.
  610. * @return bool
  611. */
  612. public function __isset( $key ) {
  613. return in_array( $key, array( 'locations', 'json' ), true );
  614. }
  615. /**
  616. * Magic __get method for backwards compatibility.
  617. *
  618. * @date 24/4/20
  619. * @since 5.9.0
  620. *
  621. * @param string $key Key name.
  622. * @return mixed
  623. */
  624. public function __get( $key ) {
  625. switch ( $key ) {
  626. case 'locations':
  627. return acf_get_instance( 'ACF_Legacy_Locations' );
  628. case 'json':
  629. return acf_get_instance( 'ACF_Local_JSON' );
  630. }
  631. return null;
  632. }
  633. }
  634. /**
  635. * The main function responsible for returning the one true acf Instance to functions everywhere.
  636. * Use this function like you would a global variable, except without needing to declare the global.
  637. *
  638. * Example: <?php $acf = acf(); ?>
  639. *
  640. * @date 4/09/13
  641. * @since 4.3.0
  642. *
  643. * @return ACF
  644. */
  645. function acf() {
  646. global $acf;
  647. // Instantiate only once.
  648. if ( ! isset( $acf ) ) {
  649. $acf = new ACF();
  650. $acf->initialize();
  651. }
  652. return $acf;
  653. }
  654. // Instantiate.
  655. acf();
  656. } // class_exists check