Skip to content

Commit 58a22c3

Browse files
author
Adam Fenton
committed
Merge pull request #2 from moxie-leean/new-type
New type
2 parents 98b087a + e4ae5f1 commit 58a22c3

File tree

5 files changed

+174
-1
lines changed

5 files changed

+174
-1
lines changed

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
use Lean\Acf\Types\Gravity;
3+
4+
/*
5+
Plugin Name: Advanced Custom Fields: Gravity Forms extension
6+
Plugin URI: https://github.com/moxie-leean/wp-acf-select-gravityform
7+
Description: ACF Type that allow to have a custom gravity form type
8+
Version: 0.1.0
9+
Author: Moxie
10+
Author URI: http://getmoxied.net/
11+
*/
12+
13+
14+
$url = plugin_dir_url( __FILE__ );
15+
$path = plugin_dir_path( __FILE__ );
16+
17+
require_once $path . 'vendor/autoload.php';
18+
19+
new Gravity\Select( $url, $path );

src/Select-Forms-v5.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace Lean\Acf\Types\Gravity;
2+
3+
/**
4+
* Setup configuration of the ACF fields form.
5+
*/
6+
class Acf_Field_Select_Form extends \acf_field {
7+
8+
/**
9+
* This function will setup the field type data
10+
*/
11+
function __construct( $settings ) {
12+
/**
13+
* Single word, no spaces. Underscores allowed
14+
*/
15+
$this->name = 'SELECT_GRAVITY_FORMS';
16+
$this->label = 'Gravity Forms';
17+
$this->category = 'choice';
18+
$this->defaults = [];
19+
$this->l10n = [ 'error' => 'Select a valid option' ];
20+
$this->settings = $settings;
21+
22+
parent::__construct();
23+
}
24+
25+
/*
26+
* Create the HTML interface for your field
27+
*
28+
* @param $field (array) the $field being edited
29+
* @return n/a
30+
*/
31+
function render_field( $field ) {
32+
Select_Field::render( $field );
33+
}
34+
35+
/*
36+
* This filter is applied to the $value before it is saved in the db we make
37+
* sure we save an int value that is positive, 0 is going to be the default
38+
* value for non form so any value like null will be converted to 0.
39+
*
40+
* @param $value (mixed) the value found in the database
41+
* @param $post_id (mixed) the $post_id from which the value was loaded
42+
* @param $field (array) the field array holding all the field options
43+
* @return $value
44+
*/
45+
function update_value( $value, $post_id, $field ) {
46+
return absint( $value );
47+
}
48+
49+
/*
50+
* This filter is appied to the $value after it is loaded from the db and
51+
* before it is returned to the template. Make sure to return always an int
52+
* value since when it comes from the DB it comes as string like '1.
53+
*
54+
* @param $value (mixed) the value which was loaded from the database
55+
* @param $post_id (mixed) the $post_id from which the value was loaded
56+
* @param $field (array) the field array holding all the field options
57+
*
58+
* @return $value (mixed) the modified value
59+
*/
60+
function format_value( $value, $post_id, $field ) {
61+
return absint( $value );
62+
}
63+
}
64+
new Acf_Field_Select_Form( $this->settings );
65+
?>

src/Select.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php namespace Lean\Acf\Types\Gravity;
2+
3+
/**
4+
* Registration of the new select type
5+
*/
6+
class Select {
7+
/*
8+
* __construct
9+
*/
10+
function __construct( $url, $path ) {
11+
$this->settings = [
12+
'version' => '0.0.1',
13+
'url' => $url,
14+
'path' => $path,
15+
];
16+
add_action( 'acf/include_field_types', [ $this, 'include_field_types' ] );
17+
}
18+
19+
/*
20+
* include_field_types
21+
*
22+
* This function will include the field type class
23+
*
24+
* @param $version (int) major ACF version. Defaults to 5
25+
* @return n/a
26+
*/
27+
function include_field_types( $version = 5 ) {
28+
include_once( 'Select-Forms-v' . $version . '.php' );
29+
}
30+
}
31+
?>

src/Select_Field.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php namespace Lean\Acf\Types\Gravity;
2+
3+
/**
4+
* Definition of the markup of the field.
5+
*/
6+
class Select_Field {
7+
8+
public static function render( $field ) {
9+
if ( ! class_exists( '\GFAPI' ) ) {
10+
self::error( 'Make sure you installed and activated Gravity Forms' );
11+
return;
12+
}
13+
$forms = \GFAPI::get_forms();
14+
if ( empty( $forms ) ) {
15+
self::error( 'You don\'t have any available forms.' );
16+
return;
17+
}
18+
self::select( $forms, $field );
19+
}
20+
21+
protected static function error( $message ) {
22+
?>
23+
<strong><?php echo esc_html( $message ); ?></strong>
24+
<?php
25+
}
26+
27+
protected static function select( $forms, $field ) {
28+
?>
29+
<select name="<?php echo esc_attr( $field['name'] ); ?>">
30+
<?php
31+
self::option();
32+
$selection = isset( $field['value'] ) ? absint( $field['value'] ) : 0;
33+
foreach ( $forms as $form ) {
34+
self::option([
35+
'value' => $form['id'],
36+
'label' => $form['title'],
37+
'selected' => $selection === $form['id'],
38+
]);
39+
}
40+
?>
41+
</select>
42+
<?php
43+
}
44+
45+
protected static function option( $args = [] ) {
46+
$args = wp_parse_args( $args, [
47+
'selected' => false,
48+
'value' => 0,
49+
'label' => 'None',
50+
]);
51+
$selected = $args['selected'] ? 'selected' : '';
52+
?>
53+
<option value="<?php echo esc_attr( $args['value'] ); ?>" <?php echo esc_attr( $selected ); ?>>
54+
<?php echo esc_html( $args['label'] ); ?>
55+
</option>
56+
<?php
57+
}
58+
}

0 commit comments

Comments
 (0)