Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Feature/phpcs #100

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 79 additions & 61 deletions classes/addons/advanced-custom-fields-exclusion.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Class BEA_CSF_Addon_ACF_Exclusion
*
Expand All @@ -7,45 +8,50 @@
*
*/
class BEA_CSF_Addon_ACF_Exclusion {
static $acf_fields = array();
static $meta_data = array();
/**
* @var array
*/
public static $acf_fields = [];

/**
* @var array
*/
public static $meta_data = [];

/**
* BEA_CSF_Addon_ACF_Exclusion constructor.
*/
public function __construct() {
if ( ! class_exists( 'acf' ) ) {
return false;
return;
}

// Fields
if ( apply_filters( 'bea/csf/acf-addon-exclusion/allow-fieds-exclusion', false ) !== false ) {

add_action( 'save_post', array( __CLASS__, 'save_post_fields' ), 10, 1 );
add_action( 'acf/include_field_types', array( __CLASS__, 'acf_include_field_types' ), 9999999 );
add_filter( 'bea_csf_client_' . 'Attachment' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_fields' ), 11, 3 );
add_filter( 'bea_csf_client_' . 'PostType' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_fields' ), 11, 3 );

add_action( 'save_post', [ __CLASS__, 'save_post_fields' ], 10, 1 );
add_action( 'acf/include_field_types', [ __CLASS__, 'acf_include_field_types' ], 9999999 );
add_filter( 'bea_csf_client_Attachment_merge_data_to_transfer', [ __CLASS__, 'filter_acf_fields' ], 11, 3 );
add_filter( 'bea_csf_client_PostType_merge_data_to_transfer', [ __CLASS__, 'filter_acf_fields' ], 11, 3 );
}

// Groups
add_action( 'save_post', array( __CLASS__, 'save_post_groups' ), 10, 1 );
add_filter( 'bea_csf_client_' . 'Attachment' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_groups' ), 10, 3 );
add_filter( 'bea_csf_client_' . 'PostType' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_groups' ), 10, 3 );
add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ), 1 );
add_action( 'save_post', [ __CLASS__, 'save_post_groups' ], 10, 1 );
add_filter( 'bea_csf_client_Attachment_merge_data_to_transfer', [ __CLASS__, 'filter_acf_groups' ], 10, 3 );
add_filter( 'bea_csf_client_PostType_merge_data_to_transfer', [ __CLASS__, 'filter_acf_groups' ], 10, 3 );
add_action( 'post_edit_form_tag', [ __CLASS__, 'post_edit_form_tag' ], 1 );

// Flexible
add_action( 'save_post', array( __CLASS__, 'save_post_flexibles' ), 10, 1 );
add_filter( 'bea_csf_client_' . 'Attachment' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_flexibles' ), 10, 3 );
add_filter( 'bea_csf_client_' . 'PostType' . '_' . 'merge' . '_data_to_transfer', array( __CLASS__, 'filter_acf_flexibles' ), 10, 3 );

return true;
add_action( 'save_post', [ __CLASS__, 'save_post_flexibles' ], 10, 1 );
add_filter( 'bea_csf_client_Attachment_merge_data_to_transfer', [ __CLASS__, 'filter_acf_flexibles' ], 10, 3 );
add_filter( 'bea_csf_client_PostType_merge_data_to_transfer', [ __CLASS__, 'filter_acf_flexibles' ], 10, 3 );
}

/**
* Save field to exclude into a post meta
*
* @param $post_id
* @param int $post_id
*
* @return bool
*/
public static function save_post_fields( $post_id ) {
// verify this came from the our screen and with proper authorization,
Expand All @@ -68,7 +74,9 @@ public static function save_post_fields( $post_id ) {
/**
* Save groups to exclude into a post meta
*
* @param $post_id
* @param int $post_id
*
* @return bool
*/
public static function save_post_groups( $post_id ) {
// verify this came from the our screen and with proper authorization,
Expand All @@ -91,7 +99,9 @@ public static function save_post_groups( $post_id ) {
/**
* Save groups to exclude into a post meta
*
* @param $post_id
* @param int $post_id
*
* @return bool
*/
public static function save_post_flexibles( $post_id ) {
// verify this came from the our screen and with proper authorization,
Expand All @@ -111,31 +121,33 @@ public static function save_post_flexibles( $post_id ) {
return true;
}



/**
* Hook all ACF fields registered
*
* @return void
*/
public static function acf_include_field_types() {
foreach ( acf_get_field_types() as $sections => $fields ) {
foreach ( $fields as $field_type => $field_label ) {
add_action( 'acf/render_field/type=' . $field_type, array( __CLASS__, 'acf_render_field_before' ), 8, 1 );
add_action( 'acf/render_field/type=' . $field_type, array( __CLASS__, 'acf_render_field_after' ), 10, 1 );
add_action( 'acf/render_field/type=' . $field_type, [ __CLASS__, 'acf_render_field_before' ], 8, 1 );
add_action( 'acf/render_field/type=' . $field_type, [ __CLASS__, 'acf_render_field_after' ], 10, 1 );
}
}
}

/**
* Do nothing actually.
*
* @param $field
* @param array $field
*
* @return bool
*/
public static function acf_render_field_before( $field ) {
if ( apply_filters( 'bea/csf/acf-addon-exclusion/allow-types-fieds-exclusion', false, $field ) === false ) {
return false;
}

if ( in_array( $field['type'], array( 'flexible_content', 'repeater' ) ) ) {
if ( in_array( $field['type'], [ 'flexible_content', 'repeater' ] ) ) {
self::build_html_checkbox( $field, __( 'Exclude this group from future synchro', 'bea-content-sync-fusion' ) );
}

Expand All @@ -145,14 +157,16 @@ public static function acf_render_field_before( $field ) {
/**
* Add an checkbox after each field for exclude from future synchro
*
* @param $field
* @param array $field
*
* @return bool
*/
public static function acf_render_field_after( $field ) {
if ( apply_filters( 'bea/csf/acf-addon-exclusion/allow-types-fieds-exclusion', false, $field ) === false ) {
return false;
}

if ( ! in_array( $field['type'], array( 'flexible_content', 'repeater' ) ) ) {
if ( ! in_array( $field['type'], [ 'flexible_content', 'repeater' ] ) ) {
self::build_html_checkbox( $field, __( 'Exclude this field from future synchro', 'bea-content-sync-fusion' ) );
}

Expand All @@ -162,7 +176,7 @@ public static function acf_render_field_after( $field ) {
/**
* Helper for display the checkbox before or after ACF fields
*
* @param array $field
* @param array $field
* @param string $label
*
* @return bool
Expand All @@ -180,7 +194,7 @@ public static function build_html_checkbox( $field, $label ) {
$current_excluded_items = get_post_meta( $post->ID, 'bea_csf_exclude_acf_fields', true );

// Show checkbox
echo '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $field['name'], (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_fields[]" value="' . esc_attr( $field['name'] ) . '" />' . $label . '</label>';
echo '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $field['name'], (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_fields[]" value="' . esc_attr( $field['name'] ) . '" />' . esc_html( $label ) . '</label>';

// Call once time
wp_nonce_field( plugin_basename( __FILE__ ), 'bea_csf_exclude_acf_fields_nonce' );
Expand All @@ -191,15 +205,15 @@ public static function build_html_checkbox( $field, $label ) {
/**
* Delete metadata excluded with ACF fields exclusion form synchro
*
* @param $data
* @param $sync_receiver_blog_id
* @param $sync_fields
* @param array $data
* @param int $sync_receiver_blog_id
* @param array $sync_fields
*
* @return mixed
*/
public static function filter_acf_groups( $data, $sync_receiver_blog_id, $sync_fields ) {
$local_id = BEA_CSF_Relations::get_object_for_any( 'posttype', $data['blogid'], $sync_fields['_current_receiver_blog_id'], $data['ID'], $data['ID'] );
if ( $local_id === false ) {
if ( false === $local_id ) {
return $data;
}

Expand All @@ -214,7 +228,7 @@ public static function filter_acf_groups( $data, $sync_receiver_blog_id, $sync_f
return $data;
}

$fields = array();
$fields = [];
foreach ( $groups as $group ) {
if ( ! in_array( $group['key'], $current_excluded_groups ) ) {
continue;
Expand All @@ -231,7 +245,7 @@ public static function filter_acf_groups( $data, $sync_receiver_blog_id, $sync_f
}

// Get only fields
self::$acf_fields = array();
self::$acf_fields = [];
self::prepare_acf_fields( $fields );

// Loop on each meta
Expand All @@ -248,15 +262,15 @@ public static function filter_acf_groups( $data, $sync_receiver_blog_id, $sync_f
/**
* Delete metadata excluded with ACF fields exclusion form synchro
*
* @param $data
* @param $sync_receiver_blog_id
* @param $sync_fields
* @param array $data
* @param int $sync_receiver_blog_id
* @param array $sync_fields
*
* @return mixed
*/
public static function filter_acf_flexibles( $data, $sync_receiver_blog_id, $sync_fields ) {
$local_id = BEA_CSF_Relations::get_object_for_any( 'posttype', $data['blogid'], $sync_fields['_current_receiver_blog_id'], $data['ID'], $data['ID'] );
if ( $local_id === false ) {
if ( false === $local_id ) {
return $data;
}

Expand Down Expand Up @@ -298,16 +312,18 @@ public static function filter_acf_flexibles( $data, $sync_receiver_blog_id, $syn
* Extract from group fields only ACF field with ID database reference (recursive !)
*
* @param array $fields
*
* @return void
*/
public static function prepare_acf_fields( $fields ) {
foreach ( (array) $fields as $field ) {
self::$acf_fields[ $field['key'] ] = $field;

if ( in_array( $field['type'], array( 'flexible_content' ) ) ) { // Flexible is recursive structure with layouts
if ( in_array( $field['type'], [ 'flexible_content' ] ) ) { // Flexible is recursive structure with layouts
foreach ( $field['layouts'] as $layout_field ) {
self::prepare_acf_fields( $layout_field['sub_fields'] );
}
} elseif ( in_array( $field['type'], array( 'repeater' ) ) ) { // Repeater is recursive structure
} elseif ( in_array( $field['type'], [ 'repeater' ] ) ) { // Repeater is recursive structure
self::prepare_acf_fields( $field['sub_fields'] );
}
}
Expand All @@ -316,15 +332,15 @@ public static function prepare_acf_fields( $fields ) {
/**
* Delete metadata excluded with ACF fields exclusion form synchro
*
* @param $data
* @param $sync_receiver_blog_id
* @param $sync_fields
* @param array $data
* @param int $sync_receiver_blog_id
* @param array $sync_fields
*
* @return mixed
* @return array
*/
public static function filter_acf_fields( $data, $sync_receiver_blog_id, $sync_fields ) {
$local_id = BEA_CSF_Relations::get_object_for_any( 'posttype', $data['blogid'], $sync_fields['_current_receiver_blog_id'], $data['ID'], $data['ID'] );
if ( $local_id === false ) {
if ( false === $local_id ) {
return $data;
}

Expand All @@ -347,7 +363,7 @@ public static function filter_acf_fields( $data, $sync_receiver_blog_id, $sync_f

// Delete all metadata from flexible/repeater
$acf_field = acf_maybe_get_field( $raw_meta_value[0] );
if ( $acf_field != false && in_array( $acf_field['type'], array( 'flexible_content', 'repeater' ) ) ) {
if ( false !== $acf_field && in_array( $acf_field['type'], [ 'flexible_content', 'repeater' ] ) ) {
foreach ( (array) $data['meta_data'] as $sub_meta_key => $sub_meta_value ) {
if ( ! preg_match( '/' . preg_quote( $acf_field['name'] ) . '[\_]\d*[\_]/', $sub_meta_key ) !== false ) {
continue;
Expand All @@ -373,7 +389,7 @@ public static function filter_acf_fields( $data, $sync_receiver_blog_id, $sync_f
if ( $meta_key == $translated_acf_name ) {
// Delete all metadata from flexible/repeater
$acf_field = acf_maybe_get_field( $matches[1][0] );
if ( $acf_field != false && in_array( $acf_field['type'], array( 'flexible_content', 'repeater' ) ) ) {
if ( false !== $acf_field && in_array( $acf_field['type'], [ 'flexible_content', 'repeater' ] ) ) {
foreach ( (array) $data['meta_data'] as $sub_meta_key => $sub_meta_value ) {
if ( ! preg_match( '/' . preg_quote( $acf_field['name'] ) . '[\_]\d*[\_]/', $sub_meta_key ) !== false ) {
continue;
Expand Down Expand Up @@ -413,7 +429,9 @@ public static function get_acf_field_name( $acf_key, $translated_acf_name = '' )
foreach ( self::$meta_data as $acf_name => $raw_meta_value ) {
if ( ! empty( $translated_acf_name ) && $raw_meta_value[0] == $acf_key && strpos( $acf_name, $translated_acf_name ) !== false ) {
return str_replace( $translated_acf_name, '', $acf_name );
} elseif ( empty( $translated_acf_name ) && $raw_meta_value[0] == $acf_key ) {
}

if ( empty( $translated_acf_name ) && $raw_meta_value[0] == $acf_key ) {
return $acf_name;
}
}
Expand All @@ -423,6 +441,7 @@ public static function get_acf_field_name( $acf_key, $translated_acf_name = '' )

/**
* Hook on admin_head "as ACF" for get all metaboxes declared by this plugin and append a small checkbox
* @return bool
*/
public static function post_edit_form_tag() {
global $wp_meta_boxes;
Expand Down Expand Up @@ -452,7 +471,7 @@ public static function post_edit_form_tag() {
* @param $meta_box
* @param $label
*
* @return bool
* @return string|bool
*/
public static function get_html_checkbox_for_metabox( $meta_box, $label ) {
global $post, $wpdb;
Expand All @@ -469,7 +488,7 @@ public static function get_html_checkbox_for_metabox( $meta_box, $label ) {
// Get current checked items
$current_excluded_items = get_post_meta( $post->ID, 'bea_csf_exclude_acf_group', true );

$output = '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $acf_group_id, (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_group[]" value="' . esc_attr( $acf_group_id ) . '" />' . $label . '</label>';
$output = '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $acf_group_id, (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_group[]" value="' . esc_attr( $acf_group_id ) . '" />' . esc_html( $label ) . '</label>';
$output .= wp_nonce_field( plugin_basename( __FILE__ ), 'bea_csf_exclude_acf_group_nonce', true, false );

return $output;
Expand All @@ -478,15 +497,14 @@ public static function get_html_checkbox_for_metabox( $meta_box, $label ) {
/**
* Helper for display the checkbox before or after ACF fields
*
* @param array $field
* @param array $field
* @param string $label
* @param $layout
* @param $i
* @param $value
* @param string $layout
* @param string $i
*
* @return bool
*/
public static function build_html_checkbox_flexible( $field, $label, $layout, $i, $value ) {
public static function build_html_checkbox_flexible( $field, $label, $layout, $i ) {
global $post, $wpdb, $counter_flexible;

if ( 'acfcloneindex' === $i ) {
Expand All @@ -504,15 +522,15 @@ public static function build_html_checkbox_flexible( $field, $label, $layout, $i

// Increment counter
if ( ! isset( $counter_flexible[ $field['name'] ] ) ) {
$counter_flexible[ $field['name'] ] = -1;
$counter_flexible[ $field['name'] ] = - 1;
}
$counter_flexible[ $field['name'] ]++;
$counter_flexible[ $field['name'] ] ++;

// Build value with name + counter
$input_value = $field['name'] . '[' . $counter_flexible[ $field['name'] ] . ']';

// Show checkbox
echo '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $input_value, (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_fields_flexible[]" value="' . esc_attr( $input_value ) . '" />' . $label . '</label>';
echo '<label class="bea-csf-acf-exclusion"><input type="checkbox" ' . checked( in_array( $input_value, (array) $current_excluded_items ), true, false ) . ' name="bea_csf_exclude_acf_fields_flexible[]" value="' . esc_attr( $input_value ) . '" />' . esc_html( $label ) . '</label>';

// Call once time
wp_nonce_field( plugin_basename( __FILE__ ), 'bea_csf_exclude_acf_fields_flexible_nonce' );
Expand Down
Loading