Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: New icon picker field #4782

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ composer.lock
vendor/*
npm-debug.log

# Re-enable required composer libraries
!/vendor/kucrut/

# PHPunit config
phpunit.xml

Expand Down
39 changes: 17 additions & 22 deletions classes/PodsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public static function submit_button( $text = null, $type = 'primary large', $na
$attributes = $other_attributes;
}

$button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class );
$button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class );
$button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />';

if ( $wrap ) {
Expand Down Expand Up @@ -463,9 +463,9 @@ public static function row( $name, $value, $type = 'text', $options = null, $pod
* @since 2.0.0
*
* @param $attributes
* @param null $name
* @param null $type
* @param null $options
* @param null $name
* @param null $type
* @param null $options
*/
public static function attributes( $attributes, $name = null, $type = null, $options = null ) {

Expand Down Expand Up @@ -515,10 +515,10 @@ public static function data( $data, $name = null, $type = null, $options = null
* @since 2.0.0
*
* @param $attributes
* @param null $name
* @param null $type
* @param null $options
* @param string $classes
* @param null $name
* @param null $type
* @param null $options
* @param string $classes
*
* @return array
*/
Expand Down Expand Up @@ -1055,41 +1055,35 @@ public static function display( $type, $value = null, $name = null, $options = n
$tableless_field_types = self::tableless_field_types();

if ( method_exists( self::$loaded[ $type ], 'display_list' ) ) {
$value = call_user_func_array(
array( self::$loaded[ $type ], 'display_list' ), array(
$value = call_user_func_array( array( self::$loaded[ $type ], 'display_list' ), array(
$value,
$name,
$options,
$pod,
$id,
$traverse,
)
);
) );
} elseif ( method_exists( self::$loaded[ $type ], 'display' ) ) {
if ( is_array( $value ) && ! in_array( $type, $tableless_field_types ) ) {
foreach ( $value as $k => $display_value ) {
$value[ $k ] = call_user_func_array(
array( self::$loaded[ $type ], 'display' ), array(
$value[ $k ] = call_user_func_array( array( self::$loaded[ $type ], 'display' ), array(
$display_value,
$name,
$options,
$pod,
$id,
$traverse,
)
);
) );
}
} else {
$value = call_user_func_array(
array( self::$loaded[ $type ], 'display' ), array(
$value = call_user_func_array( array( self::$loaded[ $type ], 'display' ), array(
$value,
$name,
$options,
$pod,
$id,
$traverse,
)
);
) );
}//end if
}//end if

Expand Down Expand Up @@ -1343,8 +1337,8 @@ public static function default_value( $value, $type = 'text', $name = null, $opt
* @since 2.0.0
*
* @param $input
* @param bool $noarray
* @param bool $db_field
* @param bool $noarray
* @param bool $db_field
*
* @return mixed|string
*/
Expand Down Expand Up @@ -1573,6 +1567,7 @@ public static function field_types() {
'pick',
'boolean',
'color',
'icon',
'slug',
);

Expand Down
3 changes: 3 additions & 0 deletions classes/PodsInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ public function core() {

$this->register_pods();

// Instantiate WP Icon Picker class.
$icon = PodsForm::field_loader( 'icon' );

$avatar = PodsForm::field_loader( 'avatar' );

if ( method_exists( $avatar, 'get_avatar' ) ) {
Expand Down
214 changes: 214 additions & 0 deletions classes/fields/icon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?php
/**
* @package Pods\Fields
*/
class PodsField_Icon extends PodsField {

/**
* Field Type Identifier
*
* @var string
* @since 2.0
*/
public static $type = 'icon';

/**
* Field Type Label
*
* @var string
* @since 2.0
*/
public static $label = 'Icon Picker';

/**
* Field Type Preparation
*
* @var string
* @since 2.0
*/
public static $prepare = '%s';

/**
* Do things like register/enqueue scripts and stylesheets
*
* @since 2.0
*/
public function __construct () {
self::$label = __( 'Icon Picker', 'pods' );

// Load the icon picker library
if ( ! class_exists( 'Icon_Picker' ) ) {
require_once PODS_DIR . '/vendor/kucrut/icon-picker/icon-picker.php';
}
$icon_picker = Icon_Picker::instance();
}

/**
* Add options and set defaults to
*
*
* @return array
* @since 2.0
*/
public function options () {
$options = array(
self::$type . '_repeatable' => array(
'label' => __( 'Repeatable Field', 'pods' ),
'default' => 0,
'type' => 'boolean',
'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
'boolean_yes_label' => '',
'dependency' => true,
'developer_mode' => true
),
'output_options' => array(
'label' => __( 'Icon libraries', 'pods' ),
'group' => array(
)
),
);

return $options;
}

/**
* Define the current field's schema for DB table storage
*
* @param array $options
*
* @return string
* @since 2.0
*/
public function schema ( $options = null ) {
$schema = 'LONGTEXT';
return $schema;
}

/**
* Change the way the value of the field is displayed with Pods::get
*
* @param mixed $value
* @param string $name
* @param array $options
* @param array $pod
* @param int $id
*
* @return mixed|null|string
* @since 2.0
*/
public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
//$value = $this->strip_html( $value, $options );
//$value = icon_picker_get_icon_url( $value );

return $value;
}

/**
* Customize output of the form field
*
* @param string $name
* @param mixed $value
* @param array $options
* @param array $pod
* @param int $id
*
* @since 2.0
*/
public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
$options = (array) $options;
$form_field_type = PodsForm::$field_type;

if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
if ( pods_var( 'read_only', $options, false ) ) {
$options[ 'readonly' ] = true;
} else {
return;
}
} elseif ( ! pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
$options[ 'readonly' ] = true;
}

pods_view( PODS_DIR . 'ui/fields/icon.php', compact( array_keys( get_defined_vars() ) ) );
}

/**
* Validate a value before it's saved
*
* @param mixed $value
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param int $id
*
* @param null $params
* @return array|bool
* @since 2.0
*/
public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
$errors = array();

$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );

if ( empty( $check ) ) {
if ( pods_var( 'required', $options ) ) {
$errors[] = __( 'This field is required.', 'pods' );
}
}

if ( ! empty( $errors ) ) {
return $errors;
}

return true;
}

/**
* Change the value or perform actions after validation but before saving to the DB
*
* @param mixed $value
* @param int $id
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
* @param object $params
*
* @return mixed|string
* @since 2.0
*/
public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {

$value = array_intersect_key( $value, array( 'type' => '', 'icon' => '' ) );

if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
$value = '';
}

$value = array_map( 'strip_tags', $value );

return $value;
}

/**
* Customize the Pods UI manage table column output
*
* @param int $id
* @param mixed $value
* @param string $name
* @param array $options
* @param array $fields
* @param array $pod
*
* @return mixed|string
* @since 2.0
*/
public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
/*$value = $this->strip_html( $value, $options );

if ( 0 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) )
$value = wp_trim_words( $value );*/

return $value;
}
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"require": {
"composer/installers": "1.9.*",
"php": ">=5.3",
"freemius/wordpress-sdk": "2.4.2"
"freemius/wordpress-sdk": "2.4.2",
"kucrut/icon-picker": "^0.5.0"
},
"require-dev": {
"automattic/vipwpcs": "^2.0",
Expand All @@ -67,6 +68,9 @@
"wp-cli/wp-cli": "Enables access to Pods CLI commands."
},
"extra": {
"installer-name": "pods"
"installer-name": "pods",
"installer-paths": {
"vendor/{$vendor}/{$name}/": ["type:wordpress-plugin"]
}
}
}
27 changes: 27 additions & 0 deletions ui/fields/icon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

$wp_icon_picker = Icon_Picker::instance();
$wp_icon_picker->load();

if ( ! did_action( 'icon_picker_admin_loaded' ) ) {
$wp_icon_picker_loader = Icon_Picker_Loader::instance();
$wp_icon_picker_loader->_enqueue_assets();
//add_action( 'print_media_templates', array( $wp_icon_picker_loader, '_media_templates' ) );
//add_filter( 'media_view_strings', array( $wp_icon_picker_loader, '_media_view_strings' ) );
}

wp_enqueue_style( 'icon-picker' );
wp_enqueue_script( 'icon-picker' );

$attributes = array();
$attributes[ 'type' ] = 'text';
$attributes[ 'value' ] = $value;
$attributes[ 'tabindex' ] = 2;
$attributes = PodsForm::merge_attributes( $attributes, $name, $form_field_type, $options );

icon_picker_field( array(
'name' => $attributes['name'],
'id' => $attributes['id'],
'class' => $attributes['class'],
'value' => $attributes['value'],
) );
Loading