123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_Local_Meta' ) ) :
- class ACF_Local_Meta {
-
- var $meta = array();
-
- var $post_id = 0;
-
- function __construct() {
-
- add_filter( 'acf/pre_load_post_id', array( $this, 'pre_load_post_id' ), 1, 2 );
- add_filter( 'acf/pre_load_meta', array( $this, 'pre_load_meta' ), 1, 2 );
- add_filter( 'acf/pre_load_metadata', array( $this, 'pre_load_metadata' ), 1, 4 );
- }
-
- function add( $meta = array(), $post_id = 0, $is_main = false ) {
-
- if ( $this->is_request( $meta ) ) {
- $meta = $this->capture( $meta, $post_id );
- }
-
- $this->meta[ $post_id ] = $meta;
-
- if ( $is_main ) {
- $this->post_id = $post_id;
- }
-
- return $meta;
- }
-
- function is_request( $meta = array() ) {
- return acf_is_field_key( key( $meta ) );
- }
-
- function capture( $values = array(), $post_id = 0 ) {
-
- $this->meta[ $post_id ] = array();
-
- add_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
-
- if ( $values ) {
- acf_update_values( $values, $post_id );
- }
-
- remove_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
-
- return $this->meta[ $post_id ];
- }
-
- function capture_update_metadata( $null, $post_id, $name, $value, $hidden ) {
- $name = ( $hidden ? '_' : '' ) . $name;
- $this->meta[ $post_id ][ $name ] = $value;
-
- return true;
- }
-
- function remove( $post_id = 0 ) {
-
- unset( $this->meta[ $post_id ] );
-
- if ( $post_id === $this->post_id ) {
- $this->post_id = 0;
- }
- }
-
- function pre_load_meta( $null, $post_id ) {
- if ( isset( $this->meta[ $post_id ] ) ) {
- return $this->meta[ $post_id ];
- }
- return $null;
- }
-
- function pre_load_metadata( $null, $post_id, $name, $hidden ) {
- $name = ( $hidden ? '_' : '' ) . $name;
- if ( isset( $this->meta[ $post_id ] ) ) {
- if ( isset( $this->meta[ $post_id ][ $name ] ) ) {
- return $this->meta[ $post_id ][ $name ];
- }
- return '__return_null';
- }
- return $null;
- }
-
- function pre_load_post_id( $null, $post_id ) {
- if ( ! $post_id && $this->post_id ) {
- return $this->post_id;
- }
- return $null;
- }
- }
- endif;
- function acf_setup_meta( $meta = array(), $post_id = 0, $is_main = false ) {
- return acf_get_instance( 'ACF_Local_Meta' )->add( $meta, $post_id, $is_main );
- }
- function acf_reset_meta( $post_id = 0 ) {
- return acf_get_instance( 'ACF_Local_Meta' )->remove( $post_id );
- }
|