Skip to content

Commit

Permalink
Merge pull request #39 from justcoded/develop
Browse files Browse the repository at this point in the history
ACF fields builder support release
  • Loading branch information
aprokopenko committed Sep 18, 2018
2 parents 2526ce2 + 6a34937 commit 5fa3635
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 8 deletions.
52 changes: 52 additions & 0 deletions assets/css/acf-responsive-columns.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.acf-fields .jc_acf_fields {
float: left;
clear: none;
}

.acf-fields .jc_acf_fields_100 {
width: 100%;
margin: 0;
float: none;
clear: both;
}


@media only screen and (max-width: 600px) {
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
width: 100% !important;
}
}

@media only screen and (min-width: 600px) {
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
width: 100% !important;
}
}

@media only screen and (min-width: 992px) {
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
width: 50% !important;
}
}

@media only screen and (min-width: 1200px) {
.acf-fields .jc_acf_fields_25 {
width: 25% !important;
}

.acf-fields .jc_acf_fields_33 {
width: 33% !important;
}

.acf-fields .jc_acf_fields_50 {
width: 50% !important;
}

.acf-fields .jc_acf_fields_66 {
width: 66% !important;
}

.acf-fields .jc_acf_fields_75 {
width: 75% !important;
}
}
3 changes: 3 additions & 0 deletions assets/js/acf-flexible-collapse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jQuery(document).ready(function($) {
$('.layout').addClass('-collapsed');
});
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.0",
"johnpbloch/wordpress": ">=4.7",
"composer/installers": "~1.0"
"composer/installers": "~1.0",
"stoutlogic/acf-builder": "~1.8"
},
"license": "GPL-3.0+"
}
72 changes: 72 additions & 0 deletions framework/ACF/ACF_Definition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace JustCoded\WP\Framework\ACF;

use JustCoded\WP\Framework\Objects\Singleton;
use StoutLogic\AcfBuilder\FieldsBuilder;

/**
* Class ACF_Definition
*
* @property FieldsBuilder[] $fields
*/
abstract class ACF_Definition {
use Singleton;
use Has_ACF_Fields;

/**
* ACF_Definition constructor.
* Run init method to set fields configuraiton.
*/
protected function __construct() {
$this->init();
}

/**
* Init fields configuration method
*
* @return void
*/
abstract public function init();

/**
* Magic getter to get some field registered.
*
* @param string $name Field name.
*
* @return FieldsBuilder
* @throws \InvalidArgumentException
*/
public function __get( $name ) {
if ( isset( $this->fields[ $name ] ) ) {
return $this->fields[ $name ];
} else {
$self = static::instance();
throw new \InvalidArgumentException( get_class( $self ) . ": Field definition missing for \"{$name}\"." );
}
}

/**
* Static getter to get field registered
* by default return first field if name is not specified
*
* @param string|null $name Field name.
*
* @return FieldsBuilder
* @throws \Exception
* @throws \InvalidArgumentException
*/
public static function get( string $name = null ) {
$self = static::instance();

if ( empty( $self->fields ) ) {
throw new \Exception( get_class( $self ) . '::get() - No fields registered.' );
}
if ( is_null( $name ) ) {
$name = key( $self->fields );
}

// use magic getter to not duplicate code here.
return $self->$name;
}
}
74 changes: 74 additions & 0 deletions framework/ACF/ACF_Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace JustCoded\WP\Framework\ACF;


use StoutLogic\AcfBuilder\FieldBuilder;
use StoutLogic\AcfBuilder\FieldsBuilder;
use JustCoded\WP\Framework\Objects\Singleton;

/**
* Class ACF_Register
*
* @property FieldBuilder[] $fields
*/
abstract class ACF_Register {
use Singleton;
use Has_ACF_Fields;

/**
* ACF_Register constructor.
* - run init method to set fields configuration.
* - define acf hook to register fields
*/
protected function __construct() {
$this->init();

// init ACF hook for register fields.
add_action( 'acf/init', array( $this, 'register' ) );
}

/**
* Init fields configuration method
*
* @return void
*/
abstract public function init();

/**
* Register fields with ACF functions.
*/
public function register() {
foreach ( $this->fields as $field ) {
acf_add_local_field_group( $field->build() );
}
}

/**
* Remove standard editor from post edit screen.
*
* @param string $post_type Post type ID to remove editor from.
*/
protected function remove_content_editor( $post_type ) {
add_action( 'init', function () use ( $post_type ) {
remove_post_type_support( $post_type, 'editor' );
} );
}

/**
* ACF add_options_page function wrapper to check for exists.
*
* @param string $name Page name.
*
* @return bool
*/
public function add_options_page( $name ) {
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( $name );

return true;
}

return false;
}
}
44 changes: 44 additions & 0 deletions framework/ACF/ACF_Support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace JustCoded\WP\Framework\ACF;


use JustCoded\WP\Framework\Objects\Singleton;

class ACF_Support {
use Singleton;

/**
* Class constructor.
*/
public function __construct() {
add_filter( 'acf/input/admin_head', array( $this, 'register_assets' ) );
add_action( 'admin_menu', array( $this, 'acf_remove_ui' ) );
}

/**
* Flexible Collapse Fields and Responsive ACF Fields.
*/
public function register_assets() {
if ( is_admin() ) {
wp_enqueue_script( '_jtf-acf_collapse', jtf_plugin_url( 'assets/js/acf-flexible-collapse.js' ), [ 'jquery' ] );
wp_enqueue_style( '_jtf-acf_responsive_fields', jtf_plugin_url( 'assets/css/acf-responsive-columns.css' ) );
}
}

/**
* Remove ACF UI from menu.
*/
public function acf_remove_ui() {
remove_menu_page( 'edit.php?post_type=acf-field-group' );
}

/**
* Check that required plugin is installed and activated
*
* @return bool
*/
public static function check_requirements() {
return class_exists( 'ACF' );
}
}
76 changes: 76 additions & 0 deletions framework/ACF/Has_ACF_Fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace JustCoded\WP\Framework\ACF;

use StoutLogic\AcfBuilder\Builder;
use StoutLogic\AcfBuilder\FieldBuilder;
use StoutLogic\AcfBuilder\FieldsBuilder;

trait Has_ACF_Fields {
/**
* @var FieldsBuilder[]
*/
protected $fields;

/**
* Add field definition to internal stack.
*
* @param FieldsBuilder ...$fields FieldsBuilder objects to be added.
*/
public function has( ...$fields ) {
/* @var FieldsBuilder[] $args */
$args = func_get_args();

foreach ( $args as $group ) {
$group = $group->getRootContext();

$this->set_responsive_width_classes( $group->getFields() );

$this->fields[ $group->getName() ] = $group;
}
}

/**
* Update fields wrapper for responsive.
*
* @param FieldBuilder[] $fields Group fields.
*/
public function set_responsive_width_classes( $fields ) {
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
$wrapper = $field->getWrapper();
$width = isset( $wrapper['width'] ) ? $wrapper['width'] : '100%';
if ( false !== strpos( $width, '%' ) ) {
$width = trim( $width, '%' );
// Set attr class.
$field->setAttr( 'class', 'jc_acf_fields jc_acf_fields_' . $width );
// Set block layout for responsive.
$field->setConfig( 'layout', 'block' );
// Clear field width.
$field->setWidth( '' );
}
}
}
}

/**
* Build a fields group configuration object
*
* @param string|null $name Group name.
* @param array $group_config Group config.
*
* @return FieldsBuilder
*/
public function build( string $name = '', array $group_config = [] ) {
// take current class short name as field name.
if ( empty( $name ) ) {
$class_name_parts = explode( '\\', get_class( $this ) );
$class_short_name = strtolower( end( $class_name_parts ) );
if ( ! isset( $this->fields[ $class_short_name ] ) ) {
$name = $class_short_name;
}
}

return new FieldsBuilder( $name, $group_config );
}
}
52 changes: 52 additions & 0 deletions framework/Objects/Thememeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace JustCoded\WP\Framework\Objects;

/**
* Class Thememeta
* Get theme option fields
*/
class Thememeta extends Meta {

/**
* Get post id
*
* @return false|int
*/
public function get_object_id() {
return 'option';
}

/**
* Getter of postmeta from advanced custom fields
*
* @param string $field_name Field name to get.
* @param int $post_id Post ID if different from get_the_ID.
* @param bool|string $format_value Format value or not.
*
* @return mixed
* @throws \Exception Unsupported custom fields plugin.
*/
public function get_value_acf( $field_name, $post_id, $format_value ) {
return get_field( $field_name, $post_id, $format_value );
}

/**
* Getter of postmeta from just custom fields
*
* @param string $field_name Field name to get.
* @param int $post_id Post ID if different from get_the_ID.
* @param bool|string $format_value Format value or not.
*
* @return mixed
* @throws \Exception Unsupported custom fields plugin.
*/
public function get_value_jcf( $field_name, $post_id, $format_value ) {
if ( class_exists( 'TitanFramework' ) ) {
$options = \TitanFramework::getInstance( 'just_theme_options' );

return $options->getOption( $field_name );
}
}

}

0 comments on commit 5fa3635

Please sign in to comment.