Skip to content

Commit 04c763b

Browse files
authored
Merge pull request #8 from AtomicSmash/release/1.3.0
Release/1.3.0
2 parents 597da74 + 4398b57 commit 04c763b

14 files changed

+705
-1990
lines changed

acf-limiter.php

100755100644
Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,98 @@
11
<?php
2+
23
/*
34
Plugin Name: Advanced Custom Fields: Limiter
45
Plugin URI: http://wordpress.org/extend/plugins/advanced-custom-fields-limiter-field/
56
Description: This field provides a textarea that limits the number of characters the a user can add. The limit is cleanly represented by a jQuery Ui progress bar. You can define the number of characters on a per field basis.
6-
Version: 1.2.1
7+
Version: 1.3.0
78
Author: Atomic Smash - David Darke
89
Author URI: atomicsmash.co.uk
910
License: GPLv2 or later
1011
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1112
*/
1213

13-
load_plugin_textdomain('acf-limiter', false, dirname(plugin_basename(__FILE__)) . '/lang/');
14+
// exit if accessed directly
15+
if (! defined('ABSPATH')) {
16+
exit;
17+
}
18+
1419

20+
// check if class already exists
21+
if (!class_exists('atomic_smash_acf_plugin_limiter')) :
1522

16-
function include_field_types_limiter($version)
23+
class atomic_smash_acf_plugin_limiter
1724
{
1825

19-
include_once('limiter-v5.php');
20-
}
26+
// vars
27+
public $settings;
2128

22-
add_action('acf/include_field_types', 'include_field_types_limiter');
2329

30+
/**
31+
* __construct
32+
*
33+
* This function will setup the class functionality
34+
*
35+
* @type function
36+
* @date 17/02/2016
37+
* @since 1.0.0
38+
*
39+
* @param void
40+
* @return void
41+
*/
2442

25-
function register_fields_limiter()
26-
{
27-
include_once('limiter-v4.php');
43+
public function __construct()
44+
{
45+
46+
// settings
47+
// - these will be passed into the field class.
48+
$this->settings = array(
49+
'version' => '1.3.0',
50+
'url' => plugin_dir_url(__FILE__),
51+
'path' => plugin_dir_path(__FILE__)
52+
);
53+
54+
55+
// include field
56+
add_action('acf/include_field_types', array($this, 'include_field')); // v5
57+
add_action('acf/register_fields', array($this, 'include_field')); // v4
58+
}
59+
60+
61+
/**
62+
* include_field
63+
*
64+
* This function will include the field type class
65+
*
66+
* @type function
67+
* @date 17/02/2016
68+
* @since 1.0.0
69+
*
70+
* @param $version (int) major ACF version. Defaults to false
71+
* @return void
72+
*/
73+
74+
public function include_field($version = false)
75+
{
76+
77+
// support empty $version
78+
if (!$version) {
79+
$version = 4;
80+
}
81+
82+
83+
// load acf-limiter-field
84+
load_plugin_textdomain('acf-limiter-field', false, plugin_basename(dirname(__FILE__)) . '/lang');
85+
86+
87+
// include
88+
include_once('fields/class-atomic-smash-acf-field-limiter-v' . $version . '.php');
89+
}
2890
}
2991

30-
add_action('acf/register_fields', 'register_fields_limiter');
92+
93+
// initialize
94+
new atomic_smash_acf_plugin_limiter();
95+
96+
97+
// class_exists check
98+
endif;

assets/css/limiter.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.counterWrapper {
2+
height: 0px;
3+
top: -23px;
4+
position: relative;
5+
left: 8px;
6+
color: #333;
7+
margin-bottom: 10px;
8+
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier,
9+
monospace;
10+
}

assets/js/limiter.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
(function ($) {
2+
function characterLimit(currentField) {
3+
progressBar = $(currentField).next(".progressBar");
4+
5+
chars = $(currentField)[0].value.length;
6+
7+
limit = $(currentField).data("characterlimit");
8+
9+
if (chars > limit) {
10+
currentField.value = currentField.value.substr(0, limit);
11+
chars = limit;
12+
}
13+
14+
charactersRemaining = limit - chars;
15+
16+
$(currentField)
17+
.next()
18+
.next(".counterWrapper")
19+
.children(".limiterCount")
20+
.html(chars);
21+
22+
percentage = Math.floor((chars / limit) * 100);
23+
24+
$(progressBar).progressbar({
25+
value: percentage,
26+
});
27+
}
28+
29+
function initialize_field($el) {
30+
//$el.doStuff();
31+
32+
//Setup progress bars of all limiter fields
33+
34+
$($el)
35+
.find(".limiterField")
36+
.each(function () {
37+
characterLimit(this);
38+
});
39+
40+
//Run characterLimit() when the field is being used
41+
$($el)
42+
.find(".limiterField")
43+
.on("keyup focus", function () {
44+
characterLimit(this);
45+
});
46+
}
47+
48+
if (typeof acf.add_action !== "undefined") {
49+
/**
50+
* ready append (ACF5)
51+
*
52+
* These are 2 events which are fired during the page load
53+
* ready = on page load similar to $(document).ready()
54+
* append = on new DOM elements appended via repeater field
55+
*
56+
* @type event
57+
* @date 20/07/13
58+
*
59+
* @param $el (jQuery selection) the jQuery element which contains the ACF fields
60+
* @return n/a
61+
*/
62+
63+
acf.add_action("ready append", function ($el) {
64+
// search $el for fields of type 'limiter'
65+
acf.get_fields({ type: "limiter" }, $el).each(function () {
66+
initialize_field($(this));
67+
});
68+
});
69+
} else {
70+
/**
71+
* acf/setup_fields (ACF4)
72+
*
73+
* This event is triggered when ACF adds any new elements to the DOM.
74+
*
75+
* @type function
76+
* @since 1.0.0
77+
* @date 01/01/12
78+
*
79+
* @param event e: an event object. This can be ignored
80+
* @param Element postbox: An element which contains the new HTML
81+
*
82+
* @return n/a
83+
*/
84+
85+
$(document).on("acf/setup_fields", function (e, postbox) {
86+
$(postbox)
87+
.find('.field[data-field_type="limiter"]')
88+
.each(function () {
89+
initialize_field($(this));
90+
});
91+
});
92+
}
93+
})(jQuery);

css/limiter.css

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)