Skip to content

Commit

Permalink
bump version to 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
timiwahalahti committed Aug 26, 2021
1 parent b46a651 commit b03f27c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions acf-field-network-post-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
14 changes: 0 additions & 14 deletions assets/css/input.css

This file was deleted.

68 changes: 41 additions & 27 deletions class-sippis-acf-field-network-post-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function __construct( $settings ) {
'post_type' => [],
'taxonomy' => [],
'allow_null' => false,
'multiple' => false,
'ui' => true,
];

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 . ' <span class="afc-network-post-select-site">(' . $site->blogname . ')</span>';
$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);
Expand Down Expand Up @@ -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 );
Expand All @@ -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

Expand All @@ -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

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b03f27c

Please sign in to comment.