1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316:
<?php
class Inbound_Mailer_ACF {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_filter( 'acf/location/rule_match/template_id' , array( __CLASS__ , 'load_acf_on_template' ) , 10 , 3 );
add_filter( 'acf/load_value' , array( __CLASS__ , 'load_value' ) , 10 , 3 );
add_filter('acf/location/rule_types', array( __CLASS__ , 'define_location_rule_types' ) );
add_filter('acf/location/rule_values/template_id', array( __CLASS__ , 'define_location_rule_values' ) );
}
public static function define_location_rule_types( $choices ) {
if (!isset($choices['Basic']['template_id'])) {
$choices['Basic']['template_id'] = __('Template ID', 'landing-page');
}
return $choices;
}
public static function define_location_rule_values( $choices ) {
$uploaded_templates = Inbound_Mailer_Load_Templates::get_uploaded_templates();
$core_templates = Inbound_Mailer_Load_Templates::get_core_templates();
$template_ids = $uploaded_templates + $core_templates;
if( $template_ids ) {
foreach( $template_ids as $template_id ) {
$choices[ $template_id ] = $template_id;
}
}
return $choices;
}
public static function load_value( $value, $post_id, $field ) {
global $post;
if ( !isset($post) || $post->post_type != 'inbound-email' ) {
return $value;
}
$vid = Inbound_Mailer_Variations::get_current_variation_id();
$settings = get_post_meta( $post_id , 'inbound_settings' , true);
$variations = ( isset($settings['variations']) ) ? $settings['variations'] : null;
if (!$variations) {
return $value;
}
if ( isset( $variations[ $vid ][ 'acf' ] ) ) {
$new_value = self::search_field_array( $variations[ $vid ][ 'acf' ] , $field );
if (!is_array($value) && is_array($new_value)) {
$value = count($new_value);
} else if( $new_value) {
$value = $new_value;
}
if ( !is_admin() && is_string($value) ) {
$value = do_shortcode($value);
}
}
return self::format_value( $value , $field );
}
public static function format_value( $value , $field ){
if ($field['type']=='color_picker' && is_array($value)){
$value = $value[1];
if (!strstr($value,'#')) {
$value = $field['default_value'];
}
}
return $value;
}
public static function search_field_array( $array , $field ) {
$needle = $field['key'];
foreach ($array as $key => $value ){
if ($key === $needle && !is_array($value) ) {
return $value;
}
if ( is_array($value) ) {
if ( $key === $needle ) {
$repeater_array = self::get_repeater_layouts( $value );
if ($repeater_array) {
return $repeater_array;
} else {
return $value;
}
}
$repeater_value = self::get_repeater_values( $value , $field );
if ($repeater_value) {
return $repeater_value;
}
}
}
return false;
}
public static function get_repeater_layouts( $array ) {
$fields = array();
foreach ($array as $key => $value) {
if ( isset( $value['acf_fc_layout'] ) ) {
$fields[] = $value['acf_fc_layout'];
}
}
return $fields;
}
public static function get_repeater_values( $array , $field ) {
preg_match('/(_\d_)/', $field['name'], $matches, 0);
if (!$matches) {
return false;
}
$pointer = str_replace('_' , '' , $matches[0]);
$repeater_key = self::key_search($array, $field , true );
if (isset($array[$repeater_key][$pointer][$field['key']])){
return $array[$repeater_key][$pointer][$field['key']];
}
return '';
}
public static function load_acf_on_template( $allow , $rule, $args ) {
global $post;
if (!isset($post) || $post->post_type != 'inbound-email' ) {
return $allow;
}
$template = Inbound_Mailer_Variations::get_current_template( $args['post_id'] );
if ($template == $rule['value']) {
return true;
} else {
return false;
}
}
public static function load_javascript_on_admin_edit_post_page() {
global $parent_file;
if ( $parent_file == 'edit.php?post_type=inbound-email' ) {
echo "
<script>
jQuery('#publish').on('click', function() {
jQuery('#publish').removeClass('disabled');
jQuery('.spinner').show();
jQuery('#publish').val('".__('Saving','inbound-email')."');
});
</script>
";
}
}
public static function key_search($array, $field , $get_parent = false , $last_key = false) {
$value = false;
foreach ($array as $key => $item) {
if ($key === $field['key'] ) {
$value = $item;
} else {
if (is_array($item)) {
$last_key = ( !is_numeric($key)) ? $key : $last_key;
$value = self::key_search($item, $field , $get_parent , $last_key );
}
}
if ($value) {
if (!$get_parent) {
return $value;
} else {
return $last_key;
}
}
}
return false;
}
}
if (!function_exists('inbound_mailer_acf_integration')) {
add_action( 'init' , 'inbound_mailer_acf_integration' );
function inbound_mailer_acf_integration() {
new Inbound_Mailer_ACF();
}
}