Skip to content

Commit 5fa3635

Browse files
authored
Merge pull request #39 from justcoded/develop
ACF fields builder support release
2 parents 2526ce2 + 6a34937 commit 5fa3635

File tree

11 files changed

+380
-8
lines changed

11 files changed

+380
-8
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.acf-fields .jc_acf_fields {
2+
float: left;
3+
clear: none;
4+
}
5+
6+
.acf-fields .jc_acf_fields_100 {
7+
width: 100%;
8+
margin: 0;
9+
float: none;
10+
clear: both;
11+
}
12+
13+
14+
@media only screen and (max-width: 600px) {
15+
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
16+
width: 100% !important;
17+
}
18+
}
19+
20+
@media only screen and (min-width: 600px) {
21+
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
22+
width: 100% !important;
23+
}
24+
}
25+
26+
@media only screen and (min-width: 992px) {
27+
.acf-fields .jc_acf_fields_25, .jc_acf_fields_33, .jc_acf_fields_50, .jc_acf_fields_66, .jc_acf_fields_75 {
28+
width: 50% !important;
29+
}
30+
}
31+
32+
@media only screen and (min-width: 1200px) {
33+
.acf-fields .jc_acf_fields_25 {
34+
width: 25% !important;
35+
}
36+
37+
.acf-fields .jc_acf_fields_33 {
38+
width: 33% !important;
39+
}
40+
41+
.acf-fields .jc_acf_fields_50 {
42+
width: 50% !important;
43+
}
44+
45+
.acf-fields .jc_acf_fields_66 {
46+
width: 66% !important;
47+
}
48+
49+
.acf-fields .jc_acf_fields_75 {
50+
width: 75% !important;
51+
}
52+
}

assets/js/acf-flexible-collapse.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
jQuery(document).ready(function($) {
2+
$('.layout').addClass('-collapsed');
3+
});

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": ">=7.0",
2020
"johnpbloch/wordpress": ">=4.7",
21-
"composer/installers": "~1.0"
21+
"composer/installers": "~1.0",
22+
"stoutlogic/acf-builder": "~1.8"
2223
},
2324
"license": "GPL-3.0+"
2425
}

framework/ACF/ACF_Definition.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\ACF;
4+
5+
use JustCoded\WP\Framework\Objects\Singleton;
6+
use StoutLogic\AcfBuilder\FieldsBuilder;
7+
8+
/**
9+
* Class ACF_Definition
10+
*
11+
* @property FieldsBuilder[] $fields
12+
*/
13+
abstract class ACF_Definition {
14+
use Singleton;
15+
use Has_ACF_Fields;
16+
17+
/**
18+
* ACF_Definition constructor.
19+
* Run init method to set fields configuraiton.
20+
*/
21+
protected function __construct() {
22+
$this->init();
23+
}
24+
25+
/**
26+
* Init fields configuration method
27+
*
28+
* @return void
29+
*/
30+
abstract public function init();
31+
32+
/**
33+
* Magic getter to get some field registered.
34+
*
35+
* @param string $name Field name.
36+
*
37+
* @return FieldsBuilder
38+
* @throws \InvalidArgumentException
39+
*/
40+
public function __get( $name ) {
41+
if ( isset( $this->fields[ $name ] ) ) {
42+
return $this->fields[ $name ];
43+
} else {
44+
$self = static::instance();
45+
throw new \InvalidArgumentException( get_class( $self ) . ": Field definition missing for \"{$name}\"." );
46+
}
47+
}
48+
49+
/**
50+
* Static getter to get field registered
51+
* by default return first field if name is not specified
52+
*
53+
* @param string|null $name Field name.
54+
*
55+
* @return FieldsBuilder
56+
* @throws \Exception
57+
* @throws \InvalidArgumentException
58+
*/
59+
public static function get( string $name = null ) {
60+
$self = static::instance();
61+
62+
if ( empty( $self->fields ) ) {
63+
throw new \Exception( get_class( $self ) . '::get() - No fields registered.' );
64+
}
65+
if ( is_null( $name ) ) {
66+
$name = key( $self->fields );
67+
}
68+
69+
// use magic getter to not duplicate code here.
70+
return $self->$name;
71+
}
72+
}

framework/ACF/ACF_Register.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\ACF;
4+
5+
6+
use StoutLogic\AcfBuilder\FieldBuilder;
7+
use StoutLogic\AcfBuilder\FieldsBuilder;
8+
use JustCoded\WP\Framework\Objects\Singleton;
9+
10+
/**
11+
* Class ACF_Register
12+
*
13+
* @property FieldBuilder[] $fields
14+
*/
15+
abstract class ACF_Register {
16+
use Singleton;
17+
use Has_ACF_Fields;
18+
19+
/**
20+
* ACF_Register constructor.
21+
* - run init method to set fields configuration.
22+
* - define acf hook to register fields
23+
*/
24+
protected function __construct() {
25+
$this->init();
26+
27+
// init ACF hook for register fields.
28+
add_action( 'acf/init', array( $this, 'register' ) );
29+
}
30+
31+
/**
32+
* Init fields configuration method
33+
*
34+
* @return void
35+
*/
36+
abstract public function init();
37+
38+
/**
39+
* Register fields with ACF functions.
40+
*/
41+
public function register() {
42+
foreach ( $this->fields as $field ) {
43+
acf_add_local_field_group( $field->build() );
44+
}
45+
}
46+
47+
/**
48+
* Remove standard editor from post edit screen.
49+
*
50+
* @param string $post_type Post type ID to remove editor from.
51+
*/
52+
protected function remove_content_editor( $post_type ) {
53+
add_action( 'init', function () use ( $post_type ) {
54+
remove_post_type_support( $post_type, 'editor' );
55+
} );
56+
}
57+
58+
/**
59+
* ACF add_options_page function wrapper to check for exists.
60+
*
61+
* @param string $name Page name.
62+
*
63+
* @return bool
64+
*/
65+
public function add_options_page( $name ) {
66+
if ( function_exists( 'acf_add_options_page' ) ) {
67+
acf_add_options_page( $name );
68+
69+
return true;
70+
}
71+
72+
return false;
73+
}
74+
}

framework/ACF/ACF_Support.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\ACF;
4+
5+
6+
use JustCoded\WP\Framework\Objects\Singleton;
7+
8+
class ACF_Support {
9+
use Singleton;
10+
11+
/**
12+
* Class constructor.
13+
*/
14+
public function __construct() {
15+
add_filter( 'acf/input/admin_head', array( $this, 'register_assets' ) );
16+
add_action( 'admin_menu', array( $this, 'acf_remove_ui' ) );
17+
}
18+
19+
/**
20+
* Flexible Collapse Fields and Responsive ACF Fields.
21+
*/
22+
public function register_assets() {
23+
if ( is_admin() ) {
24+
wp_enqueue_script( '_jtf-acf_collapse', jtf_plugin_url( 'assets/js/acf-flexible-collapse.js' ), [ 'jquery' ] );
25+
wp_enqueue_style( '_jtf-acf_responsive_fields', jtf_plugin_url( 'assets/css/acf-responsive-columns.css' ) );
26+
}
27+
}
28+
29+
/**
30+
* Remove ACF UI from menu.
31+
*/
32+
public function acf_remove_ui() {
33+
remove_menu_page( 'edit.php?post_type=acf-field-group' );
34+
}
35+
36+
/**
37+
* Check that required plugin is installed and activated
38+
*
39+
* @return bool
40+
*/
41+
public static function check_requirements() {
42+
return class_exists( 'ACF' );
43+
}
44+
}

framework/ACF/Has_ACF_Fields.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\ACF;
4+
5+
use StoutLogic\AcfBuilder\Builder;
6+
use StoutLogic\AcfBuilder\FieldBuilder;
7+
use StoutLogic\AcfBuilder\FieldsBuilder;
8+
9+
trait Has_ACF_Fields {
10+
/**
11+
* @var FieldsBuilder[]
12+
*/
13+
protected $fields;
14+
15+
/**
16+
* Add field definition to internal stack.
17+
*
18+
* @param FieldsBuilder ...$fields FieldsBuilder objects to be added.
19+
*/
20+
public function has( ...$fields ) {
21+
/* @var FieldsBuilder[] $args */
22+
$args = func_get_args();
23+
24+
foreach ( $args as $group ) {
25+
$group = $group->getRootContext();
26+
27+
$this->set_responsive_width_classes( $group->getFields() );
28+
29+
$this->fields[ $group->getName() ] = $group;
30+
}
31+
}
32+
33+
/**
34+
* Update fields wrapper for responsive.
35+
*
36+
* @param FieldBuilder[] $fields Group fields.
37+
*/
38+
public function set_responsive_width_classes( $fields ) {
39+
if ( ! empty( $fields ) ) {
40+
foreach ( $fields as $field ) {
41+
$wrapper = $field->getWrapper();
42+
$width = isset( $wrapper['width'] ) ? $wrapper['width'] : '100%';
43+
if ( false !== strpos( $width, '%' ) ) {
44+
$width = trim( $width, '%' );
45+
// Set attr class.
46+
$field->setAttr( 'class', 'jc_acf_fields jc_acf_fields_' . $width );
47+
// Set block layout for responsive.
48+
$field->setConfig( 'layout', 'block' );
49+
// Clear field width.
50+
$field->setWidth( '' );
51+
}
52+
}
53+
}
54+
}
55+
56+
/**
57+
* Build a fields group configuration object
58+
*
59+
* @param string|null $name Group name.
60+
* @param array $group_config Group config.
61+
*
62+
* @return FieldsBuilder
63+
*/
64+
public function build( string $name = '', array $group_config = [] ) {
65+
// take current class short name as field name.
66+
if ( empty( $name ) ) {
67+
$class_name_parts = explode( '\\', get_class( $this ) );
68+
$class_short_name = strtolower( end( $class_name_parts ) );
69+
if ( ! isset( $this->fields[ $class_short_name ] ) ) {
70+
$name = $class_short_name;
71+
}
72+
}
73+
74+
return new FieldsBuilder( $name, $group_config );
75+
}
76+
}

framework/Objects/Thememeta.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\Objects;
4+
5+
/**
6+
* Class Thememeta
7+
* Get theme option fields
8+
*/
9+
class Thememeta extends Meta {
10+
11+
/**
12+
* Get post id
13+
*
14+
* @return false|int
15+
*/
16+
public function get_object_id() {
17+
return 'option';
18+
}
19+
20+
/**
21+
* Getter of postmeta from advanced custom fields
22+
*
23+
* @param string $field_name Field name to get.
24+
* @param int $post_id Post ID if different from get_the_ID.
25+
* @param bool|string $format_value Format value or not.
26+
*
27+
* @return mixed
28+
* @throws \Exception Unsupported custom fields plugin.
29+
*/
30+
public function get_value_acf( $field_name, $post_id, $format_value ) {
31+
return get_field( $field_name, $post_id, $format_value );
32+
}
33+
34+
/**
35+
* Getter of postmeta from just custom fields
36+
*
37+
* @param string $field_name Field name to get.
38+
* @param int $post_id Post ID if different from get_the_ID.
39+
* @param bool|string $format_value Format value or not.
40+
*
41+
* @return mixed
42+
* @throws \Exception Unsupported custom fields plugin.
43+
*/
44+
public function get_value_jcf( $field_name, $post_id, $format_value ) {
45+
if ( class_exists( 'TitanFramework' ) ) {
46+
$options = \TitanFramework::getInstance( 'just_theme_options' );
47+
48+
return $options->getOption( $field_name );
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)