Skip to content

Instantly share code, notes, and snippets.

@huubl
Forked from teolaz/ACFToArray.php
Created December 15, 2021 13:03
Show Gist options
  • Save huubl/58bf26251af6e4f8fa6aff4a7e3c5eff to your computer and use it in GitHub Desktop.
Save huubl/58bf26251af6e4f8fa6aff4a7e3c5eff to your computer and use it in GitHub Desktop.
This class is an helpful method to add a caching layer to Advanced Custom Fields. This works adding a new row on database with the entire ACF data serialized for that saved post or option page, and then WITH A SINGLE QUERY done you can have all data back and ready to use.
<?php
/**
* Created by PhpStorm.
* User: Matteo
* Date: 04/07/2018
* Time: 12:52
*/
/**
* Class ACFToArray
* Used to offer a better performance experience on frontend, while maintaining the same old ACF structure
*/
class ACFToArray {
const DB_FIELD_PREFIX = 'acf_cache_array';
protected function __construct() {
add_action( 'acf/save_post', array( $this, 'saveTotalArray' ), 20000 );
}
public static function getInstance() {
static $instance = null;
if ( ! $instance ) {
$instance = new static();
}
return $instance;
}
/**
* Save the array of values when save_post action is fired, so the load is high only on backend
*
* @param $post_id
*/
public function saveTotalArray( $post_id ) {
$post_info = acf_get_post_id_info( $post_id );
if ( $post_info[ 'id' ] === 0 ) {
return;
}
$post_fields = get_fields( $post_id );
if ( ! is_array( $post_fields ) || ( is_array( $post_fields ) && count( $post_fields ) == 0 ) ) {
return;
}
$meta_key = self::DB_FIELD_PREFIX;
switch ( $post_info[ 'type' ] ) {
case 'user':
$post_id = str_replace( 'user_', '', $post_id );
update_user_meta( $post_id, $meta_key, $post_fields );
break;
case 'comment':
$post_id = str_replace( 'comment_', '', $post_id );
update_comment_meta( $post_id, $meta_key, $post_fields );
break;
case 'term':
$post_id = str_replace( 'term_', '', $post_id );
update_term_meta( $post_id, $meta_key, $post_fields );
break;
case 'post':
update_post_meta( $post_id, $meta_key, $post_fields );
break;
case 'option':
$meta_key .= '_' . $post_id;
update_option( $meta_key, $post_fields, false );
break;
default:
break;
}
}
/**
* Get the saved array of ACF fields for specified post_id
*
* @param bool $post_id
*
* @return bool|mixed
*/
public function getTotalArray( $post_id = false ) {
$post_id = acf_get_valid_post_id( $post_id );
$post_info = acf_get_post_id_info( $post_id );
if ( $post_info[ 'id' ] === 0 ) {
return false;
}
$meta_key = self::DB_FIELD_PREFIX;
$fields = false;
switch ( $post_info[ 'type' ] ) {
case 'user':
$post_id = str_replace( 'user_', '', $post_id );
$fields = get_user_meta( $post_id, $meta_key );
break;
case 'comment':
$post_id = str_replace( 'comment_', '', $post_id );
$fields = get_comment_meta( $post_id, $meta_key );
break;
case 'term':
$post_id = str_replace( 'term_', '', $post_id );
$fields = get_term_meta( $post_id, $meta_key );
break;
case 'post':
$fields = get_post_meta( $post_id, $meta_key );
break;
case 'option':
$meta_key .= '_' . $post_id;
$fields = get_option( $meta_key );
break;
default:
break;
}
// fix the one-man-band problem with array of array containing only 1 element
if (
is_array( $fields ) &&
count( $fields ) == 1 &&
array_key_exists( 0, $fields )
) {
$fields = array_shift( $fields );
}
return $fields;
}
}
ACFToArray::getInstance();
function get_fields_array( $post_id = false ) {
return ACFToArray::getInstance()->getTotalArray( $post_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment