Skip to content

Commit

Permalink
* Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusforsberg committed Nov 27, 2015
0 parents commit a5baeb8
Show file tree
Hide file tree
Showing 12 changed files with 1,633 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ACF Native Fields
An interface to move native WordPress fields and options into ACF for a cleaner editor layout

## Description
A custom ACF field type which acts as a visual way to implement the functionality [described in ACF's handy tutorial](http://www.advancedcustomfields.com/resources/moving-wp-elements-content-editor-within-acf-fields/).

## Upcoming features
- Support for any metabox, both from plugins and built-in WP metaboxes
- Hooks in frontend and backend to implement custom native fields
- Options in field group editor to add own CSS/JS to execute on a native field?
59 changes: 59 additions & 0 deletions acf-native-field-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
if(!defined('ABSPATH')) exit;

class acf_field_native extends acf_field {
function __construct() {
$this->name = 'native_field';

$this->label = __('Native Field', 'acf-native-fields');

$this->defaults = array(
'value' => false, // prevents acf_render_fields() from attempting to load value
);

$this->category = 'layout';

$this->l10n = array(
'not_implemented' => __('Native Field not implemented yet.', 'acf-native-field'),
);

parent::__construct();
}

function render_field_settings($field) {
acf_render_field_setting($field, array(
'label' => __('Native Field', 'acf-native-field'),
'instructions' => __('The native WordPress field to move into this placeholder.', 'acf-native-field'),
'type' => 'select',
'name' => 'native_field',
// TODO: Implement backend and frontend functionality for custom native fields (hooks)
'choices' => array(
'content' => __('Content Editor', 'acf-native-field'),
'excerpt' => __('Excerpt', 'acf-native-field'),
'featured_image' => __('Featured Image', 'acf-native-field'),
'yoast_seo' => __('Yoast SEO', 'acf-native-field'),
'publish_box' => __('Publish Box', 'acf-native-field'),
'permalink' => __('Permalink', 'acf-native-field'),
'discussion' => __('Discussion', 'acf-native-field'),
'trackbacks' => __('Trackbacks', 'acf-native-field'),
'format' => __('Format', 'acf-native-field'),
),
));
}

function render_field($field) {?>
<div class="acf-native-field" data-native-field="<?php echo $field['native_field']; ?>">
<?php _e('Loading...', 'acf-native-field'); ?>
</div><?php
}

function input_admin_enqueue_scripts() {
wp_enqueue_script('acf-native-fields', plugins_url('/js/acf-native-fields.js', __FILE__), array('jquery'), ACF_Native_Fields::instance()->plugin_data['Version']);
wp_enqueue_style('acf-native-fields', plugins_url('/css/acf-native-fields.css', __FILE__), array(), ACF_Native_Fields::instance()->plugin_data['Version']);
}

function field_group_admin_enqueue_scripts() {
wp_enqueue_script('acf-native-fields-admin', plugins_url('/js/acf-native-fields-admin.js', __FILE__), array('jquery'), ACF_Native_Fields::instance()->plugin_data['Version']);
wp_enqueue_style('acf-native-fields-admin', plugins_url('/css/acf-native-fields-admin.css', __FILE__), array(), ACF_Native_Fields::instance()->plugin_data['Version']);
}
}
63 changes: 63 additions & 0 deletions acf-native-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
Plugin Name: ACF Native Fields
Plugin URI: https://github.com/marcusforsberg/acf-native-fields
Description: An interface to move native WordPress fields and options into ACF for a cleaner editor layout
Version: 1.0.0
Author: Marcus Forsberg
Author URI: https://forsberg.ax
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.en.html
*/

if(!defined('ABSPATH')) exit;

class ACF_Native_Fields {
static $instance = false;

public $plugin_data = null;

function __construct() {
// Init plugin (check requirements etc)
add_action('admin_init', array($this, 'admin_init'));

// Add native field type to ACF
add_action('acf/include_field_types', array($this, 'include_native_field_type'));
}

public function admin_init() {
load_plugin_textdomain('acf-native-fields', false, dirname(plugin_basename(__FILE__)) . '/lang/');

$this->plugin_data = get_plugin_data(dirname(__FILE__));

// Require ACF
if (current_user_can('activate_plugins') && !is_plugin_active('advanced-custom-fields/acf.php') && !is_plugin_active('advanced-custom-fields-pro/acf.php')) {
add_action('admin_notices', array($this, 'require_acf'));
deactivate_plugins(plugin_basename( __FILE__ ));

if(isset($_GET['activate'])) {
unset($_GET['activate']);
}
}
}

public function require_acf() { ?>
<div class="error"><p><?php _e('ACF Native Fields requires Advanced Custom Fields v5+ to be installed and activated.', 'acf-native-fields'); ?></p></div><?php
}

public function include_native_field_type() {
require(dirname(__FILE__) . '/acf-native-field-type.php');

new acf_field_native();
}

static function &instance() {
if(false === self::$instance) {
self::$instance = new ACF_Native_Fields();
}

return self::$instance;
}
}

new ACF_Native_Fields();
4 changes: 4 additions & 0 deletions css/acf-native-fields-admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Hide name for native fields */
.acf-field-object-native-field tr[data-name="name"], .acf-field-object-native-field tr[data-name="required"] {
display: none!important;
}
21 changes: 21 additions & 0 deletions css/acf-native-fields.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Remove styling from postboxes (new class added to them with JS) */
.native-field-postbox .inside {
margin: 0;
padding: 0;
}

/* Remove styling from WYSIWYG editor*/
.acf-native-field .wp-editor-wrap {
margin-top: -20px;
}

.acf-native-field #wp-content-editor-tools {
border: none;
background: none;
}

/* Remove styling from publishing actions */
.acf-native-field #major-publishing-actions {
border: none;
background: none;
}
Empty file added index.php
Empty file.
22 changes: 22 additions & 0 deletions js/acf-native-fields-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function($) {
var acf_settings_native_field = acf.model.extend({
actions: {
'open_field': 'render',
'change_field_type': 'render'
},

render: function($el){
// bail early if not correct field type
if( $el.attr('data-type') != 'native_field' ) {

return;

}

// clear name
$el.find('.acf-field[data-name="name"] input').val('').trigger('change');

console.log('native_field');
}
});
})(jQuery);
144 changes: 144 additions & 0 deletions js/acf-native-fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
(function($) {
var ACF_Native_Fields = {
editor_container: null,
native_fields: null,

/**
* Initialize the ACF Native Fields plugin
*/
init: function() {
ACF_Native_Fields.editor_container = $('#post-body');
ACF_Native_Fields.native_fields = ACF_Native_Fields.editor_container.find('.acf-native-field');

// Move all native fields into their placeholders
ACF_Native_Fields.moveNativeFields();
},

/**
* Function that actually kickstarts the process of moving native WP fields into ACF fields
*/
moveNativeFields: function() {
if(ACF_Native_Fields.native_fields.length < 1) {
return;
}

ACF_Native_Fields.native_fields.each(ACF_Native_Fields.moveNativeField);
},

/**
* Get a native field element by selector. Wrapper around jQuery.find() to do any processing that needs to be
* done for all native elements before they're moved to an ACF field.
*
* @param selector Selector to pass to jQuery.find();
*
* @return jQuery A jQuery object of the found element
*/
getNativeFieldElement: function(selector) {
return ACF_Native_Fields.handlePostbox(ACF_Native_Fields.editor_container.find(selector));
},

/**
* If the given element is a postbox (has .postbox class), do some necessary changes so that it fits the layout
*
* @param native_field jQuery object
*
* @return jQuery The same object that was passed, native_field
*/
handlePostbox: function(native_field) {
if(native_field.hasClass('postbox')) {
native_field.removeClass('postbox').addClass('native-field-postbox');
native_field.find('.handlediv, h3').remove();
}

return native_field;
},

/**
* Callback run on each ACF Native Field placeholder on the page. Finds the correct native field and moves it
* into the given ACF Native Field placeholder.
*/
moveNativeField: function() {
var native_field_placeholder = $(this).empty();
var native_field_type = native_field_placeholder.data('native-field');

// First try to find a built-in method to run for this type of native field
if(typeof ACF_Native_Fields['moveNativeField_' + native_field_type] === 'function') {
native_field_placeholder.append(ACF_Native_Fields['moveNativeField_' + native_field_type]());
// TODO: Allow custom callback code to be added in field group settings and executed here?
}
// If none exists, see if a custom one has been passed, and exists
else if(false) {
// TODO: Implement backend and frontend functionality for custom native fields (hooks)
}
// If no built-in or custom method exists, give up and show a message about the problem instead.
else {
native_field_placeholder.html(acf._e('native_field', 'not_implemented'));
}
},

/**
* ACF Native Field type: WordPress content editor
*/
moveNativeField_content: function() {
return ACF_Native_Fields.getNativeFieldElement('#postdivrich');
},

/**
* ACF Native Field type: WordPress excerpt editor
*/
moveNativeField_excerpt: function() {
return ACF_Native_Fields.getNativeFieldElement('#postexcerpt');
},

/**
* ACF Native Field type: WordPress featured image
*/
moveNativeField_featured_image: function() {
return ACF_Native_Fields.getNativeFieldElement('#postimagediv');
},

/**
* ACF Native Field type: Yoast SEO meta box
*/
moveNativeField_yoast_seo: function() {
return ACF_Native_Fields.getNativeFieldElement('#wpseo_meta');
},

/**
* ACF Native Field type: WordPress publish meta box
*/
moveNativeField_publish_box: function() {
return ACF_Native_Fields.getNativeFieldElement('#submitdiv');
},

/**
* ACF Native Field type: WordPress permalink meta box
*/
moveNativeField_permalink: function() {
return ACF_Native_Fields.getNativeFieldElement('#slugdiv');
},

/**
* ACF Native Field type: WordPress discussion settings meta box
*/
moveNativeField_discussion: function() {
return ACF_Native_Fields.getNativeFieldElement('#commentstatusdiv');
},

/**
* ACF Native Field type: WordPress trackback settings meta box
*/
moveNativeField_trackbacks: function() {
return ACF_Native_Fields.getNativeFieldElement('#trackbacksdiv');
},

/**
* ACF Native Field type: WordPress post format meta box
*/
moveNativeField_format: function() {
return ACF_Native_Fields.getNativeFieldElement('#formatdiv');
},
};

$(document).ready(ACF_Native_Fields.init);
})(jQuery);
Loading

0 comments on commit a5baeb8

Please sign in to comment.