Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Created March 4, 2020 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaborio001/798ddcea8afdca193d3b85c8e3b3bbd7 to your computer and use it in GitHub Desktop.
Save csaborio001/798ddcea8afdca193d3b85c8e3b3bbd7 to your computer and use it in GitHub Desktop.
<?php
/**
* Encapsulates the field creation of the announcement custom post type.
*
* @package compeer
* @since 0.0.6.3.3
*/
namespace Compeer;
use \StoutLogic\AcfBuilder\FieldsBuilder;
/**
* Contains the definition of the Client class for registering ACF Fields.
*/
class AnnouncementFieldsBuilder {
/**
* Constructor, initializes the FieldsBuilder Object.
*
* @since 0.0.6.3.3
*/
public function __construct() {
$this->set_fields_builder( new FieldsBuilder( 'announcement_fields' ) );
}
/**
* Creates each of the fields that are associated with the Announcement Custom Post Type.
*
* @since 0.0.6.3.3
*/
public function build_fields() {
$this->get_fields_builder()
/** Adds the target audiences that this announcement can be directed to. */
->addSelect(
'announcement_target',
array(
'label' => __( 'Announcement Target', 'compeer' ),
'instructions' => __( 'Select the groups you would like to target. Front Page will be displayed publicly in the News section of website, all other options will be displayed in the user\'s dashboard when they log in', 'compeer' ),
'wrapper' => array(
'width' => 100,
'id' => '',
'class' => '',
),
'choices' => array(
'front_page' => 'Front Page',
'all_external_users' => 'All External Users',
'all_clients_and_volunteers' => 'All Clients and Volunteers',
'all_referrers' => 'All Referrers',
),
'default_value' => 'front_page',
)
)
/** Adds the image that will go with announcement, only needs to be displayed if announcement target is front_page. */
->addImage(
'announcement_image',
array(
'label' => __( 'The image to be displayed with the announcement', 'compeer' ),
'instructions' => __( 'Select an image to go with the announcement, optimal dimensions: 200x700px' ),
'wrapper' => array(
'width' => 50,
'id' => '',
'class' => '',
),
)
)
->conditional( 'announcement_target', '==', 'front_page' )
->addWysiwyg(
'announcement_message',
array(
'label' => __( 'Announcement Message', 'compeer' ),
'instructions' => __( 'Write the announcement that you would like to send the users selected above' ),
'wrapper' => array(
'width' => 50,
'id' => '',
'class' => '',
),
'toolbar' => 'compact',
'media_upload' => 0,
)
);
}
/**
* Invoked once the fields have been created. This finishes the process by
* associating the fields that were just built to the target CPT,
* which in this case is announcement.
*
* @since 0.0.6.3.3
*/
public function register_fields() {
$this->get_fields_builder()
->setLocation( 'post_type', '==', 'announcement' );
$fields_builder_instance = $this->get_fields_builder();
add_action(
'acf/init',
function() use ( $fields_builder_instance ) {
acf_add_local_field_group( $fields_builder_instance->build() );
}
);
}
/**
* Setter for the fields_builder property.
*
* @param string $fields_builder the new value of the fields_builder property.
*/
public function set_fields_builder( $fields_builder ) {
$this->fields_builder = $fields_builder;
return $this->get_fields_builder();
}
/**
* Getter for the fields_builder property.
*
* @return string - the value of the fields_builder property.
*/
public function get_fields_builder() {
return $this->fields_builder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment