diff --git a/CHANGELOG.md b/CHANGELOG.md index 30e9de7..b411943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Removed ### Changed +## [1.1.0] - 2021-08-26 +### Fixed +- Field behaviour when not using UI / AJAX + ## [1.0.2] - 2020-10-02 ### Fixed - field rendering when value is not set diff --git a/acf-field-network-post-select.php b/acf-field-network-post-select.php index 3c9ca37..27bcca4 100755 --- a/acf-field-network-post-select.php +++ b/acf-field-network-post-select.php @@ -3,7 +3,7 @@ Plugin Name: Advanced Custom Fields: Network posts select field Plugin URI: https://github.com/timiwahalahti/acf-field-post-object-network/ Description: Adds a ACF field that allows selecting posts across the network sites. -Version: 1.0.2 +Version: 1.1.0 Author: Timi Wahalahti Author URI: https://sipp.is License: GPLv3 or later @@ -17,22 +17,22 @@ class sippis_acf_plugin_network_post_select { - var $settings; + var $settings; - function __construct() { - $this->settings = array( - 'version' => '1.0.2', - 'url' => plugin_dir_url( __FILE__ ), - 'path' => plugin_dir_path( __FILE__ ) - ); + function __construct() { + $this->settings = array( + 'version' => '1.1.0', + 'url' => plugin_dir_url( __FILE__ ), + 'path' => plugin_dir_path( __FILE__ ) + ); - add_action( 'acf/include_field_types', array( $this, 'include_field' ) ); - } // end __construct + add_action( 'acf/include_field_types', array( $this, 'include_field' ) ); + } // end __construct - function include_field( $version = false ) { - load_plugin_textdomain( 'sippis-acf-field-network-post-select', false, plugin_basename( dirname( __FILE__ ) ) . '/lang' ); - include_once( 'class-sippis-acf-field-network-post-select.php'); - } // end include_field + function include_field( $version = false ) { + load_plugin_textdomain( 'sippis-acf-field-network-post-select', false, plugin_basename( dirname( __FILE__ ) ) . '/lang' ); + include_once( 'class-sippis-acf-field-network-post-select.php' ); + } // end include_field } // end class new sippis_acf_plugin_network_post_select(); diff --git a/assets/css/input.css b/assets/css/input.css deleted file mode 100755 index 720acf8..0000000 --- a/assets/css/input.css +++ /dev/null @@ -1,14 +0,0 @@ -/* -* @Author: Timi Wahalahti -* @Date: 2020-10-01 12:58:46 -* @Last Modified by: Timi Wahalahti -* @Last Modified time: 2020-10-01 17:14:28 -*/ - -.select2-container .select2-dropdown span.afc-network-post-select-site { - display: none; -} - -.acf-field-network-post-select span.afc-network-post-select-site { - color: #595959; -} diff --git a/class-sippis-acf-field-network-post-select.php b/class-sippis-acf-field-network-post-select.php index 1f31aae..456505b 100755 --- a/class-sippis-acf-field-network-post-select.php +++ b/class-sippis-acf-field-network-post-select.php @@ -14,6 +14,7 @@ function __construct( $settings ) { 'post_type' => [], 'taxonomy' => [], 'allow_null' => false, + 'multiple' => false, 'ui' => true, ]; @@ -53,6 +54,8 @@ function get_ajax_query( $options = [] ) { 'paged' => true, ] ); + // var_dump( $options ); + // load field $field = acf_get_field( $options['field_key'] ); if ( ! $field ) { @@ -203,7 +206,7 @@ function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) { $current_site_id = get_current_blog_id(); // switch to correct site for getting the title from right post - switch_to_blog( $field['value']['site_id'] ); + switch_to_blog( $post->blog_id ); // get post_id if ( ! $post_id ) { @@ -213,8 +216,8 @@ function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) { $title = acf_get_post_title( $post, $is_search ); // add site name to title - $site = get_blog_details( $field['value']['site_id'] ); - $title = $title . ' (' . $site->blogname . ')'; + $site = get_blog_details( $post->blog_id ); + $title = $title . ' (' . $site->blogname . ')'; // filters $title = apply_filters('acf/fields/network_post_select/result', $title, $post, $field, $post_id); @@ -272,20 +275,36 @@ function update_value( $value, $post_id, $field ) { * @return array */ function get_posts( $value, $field ) { - if ( empty( $value ) ) { - return false; + $posts = []; + + // allowed sites + $get_sites_args = []; + if ( ! empty( $field['site_id'] ) ) { + $get_sites_args['site__in'] = acf_get_array( $field['site_id'] ); } + // get sites in network + $sites = get_sites( $get_sites_args ); + + // store current site id $current_site_id = get_current_blog_id(); - // switch to correct site for getting the posts from correct site - switch_to_blog( $value['site_id'] ); + // loop sites + foreach ( $sites as $site ) { + switch_to_blog( $site->blog_id ); - // get posts - $posts = acf_get_posts( [ - 'post__in' => $value, - 'post_type' => $field['post_type'] - ] ); + // get posts + $new_posts = acf_get_posts( [ + 'post__in' => $value, + 'post_type' => $field['post_type'], + ] ); + + foreach ( $new_posts as $key => $new_post ) { + $new_posts[ $key ]->blog_id = $site->blog_id; + } + + $posts = array_merge( $posts, $new_posts ); + } // switch back to current site switch_to_blog( $current_site_id ); @@ -305,23 +324,21 @@ function render_field( $field ) { $field['ajax'] = true; $field['choices'] = []; - if ( ! empty( $field['value'] ) ) { - // try to get posts based on field value - $posts = $this->get_posts( $field['value'], $field ); + // try to get posts based on field value + $posts = $this->get_posts( $field['value'], $field ); - if ( $posts ) { - foreach ( array_keys( $posts ) as $i ) { - $post = acf_extract_var( $posts, $i ); + if ( $posts ) { + foreach ( array_keys( $posts ) as $i ) { + $post = acf_extract_var( $posts, $i ); - // add posts found to choices available without select2 - $field['choices'][ $field['value']['site_id'] . '|' . $post->ID ] = $this->get_post_title( $post, $field ); - } + // add posts found to choices available without select2 + $field['choices'][ $post->blog_id . '|' . $post->ID ] = $this->get_post_title( $post, $field ); } - - // change field value format so it's in same format with AJAX query return - $field['value'] = $field['value']['site_id'] . '|' . $field['value']['post_id']; } + // change field value format so it's in same format with AJAX query return + $field['value'] = $field['value']['site_id'] . '|' . $field['value']['post_id']; + acf_render_field( $field ); } // end render_field @@ -331,9 +348,6 @@ function input_admin_enqueue_scripts() { wp_register_script( 'acf-field-network-post-select', "{$url}assets/js/input.js", [ 'acf-input' ], $version ); wp_enqueue_script( 'acf-field-network-post-select' ); - - wp_register_style( 'acf-field-network-post-select', "{$url}assets/css/input.css", [ 'acf-input' ], $version ); - wp_enqueue_style( 'acf-field-network-post-select' ); } // end input_admin_enqueue_scripts /** diff --git a/composer.json b/composer.json index 93d332b..0a1c7d9 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "homepage": "https://github.com/timiwahalahti/acf-field-network-post-select", "keywords": ["wordpress", "plugin"], "license": "GPLv3", - "version": "1.0.2", + "version": "1.1.0", "authors": [ { "name": "Timi Wahalahti",