Skip to content

gppa-colorpicker-choice-template.php: Added Color Picker choice template for GPPA to work with Jet Sloth Color Picker plugin. #1081

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

Merged
merged 2 commits into from
May 1, 2025
Merged
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
54 changes: 54 additions & 0 deletions gp-populate-anything/gppa-colorpicker-choice-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Gravity Perks // Populate Anything // Add Custom Template Row for Color Picker
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instructions Video: https://www.loom.com/share/c062a781a86242de9f1ebb92492a408c
*
* Adds "Color Picker" template row to the Populate Anything interface and processes its value (with Jet Sloth's Color Picker plugin).
*
* Plugin Name: GP Populate Anything — Color Picker Choice Template
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Adds a custom template row and value processing for "Color Picker" in Populate Anything.
* Author: Gravity Wiz
* Version: 1.0
*/
Comment on lines +1 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add security guard and text domain header
It’s best practice to prevent direct access by checking ABSPATH at the top of the file, and to include a Text Domain (and optionally Domain Path) in the plugin header so translations work correctly.

Example diff:

1a2,5
+ if ( ! defined( 'ABSPATH' ) ) {
+     exit; // Exit if accessed directly.
+ }
15a20,21
+ * Text Domain:   gp-populate-anything
+ * Domain Path:   /languages
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<?php
/**
* Gravity Perks // Populate Anything // Add Custom Template Row for Color Picker
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instructions Video: https://www.loom.com/share/c062a781a86242de9f1ebb92492a408c
*
* Adds "Color Picker" template row to the Populate Anything interface and processes its value (with Jet Sloth's Color Picker plugin).
*
* Plugin Name: GP Populate Anything — Color Picker Choice Template
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Adds a custom template row and value processing for "Color Picker" in Populate Anything.
* Author: Gravity Wiz
* Version: 1.0
*/
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Gravity Perks // Populate Anything // Add Custom Template Row for Color Picker
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instructions Video: https://www.loom.com/share/c062a781a86242de9f1ebb92492a408c
*
* Adds "Color Picker" template row to the Populate Anything interface and processes its value (with Jet Sloth's Color Picker plugin).
*
* Plugin Name: GP Populate Anything — Color Picker Choice Template
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Adds a custom template row and value processing for "Color Picker" in Populate Anything.
* Author: Gravity Wiz
* Version: 1.0
* Text Domain: gp-populate-anything
* Domain Path: /languages
*/

class GPPA_Compatibility_JetSloth_Color_Picker_Template {

public function __construct() {
add_filter( 'gppa_input_choice', array( $this, 'add_colorpicker_to_choice' ), 10, 4 );
add_action( 'gform_editor_js', array( $this, 'add_colorpicker_choice_template' ), 1 );
}

public function add_colorpicker_to_choice( $choice, $field, $object, $objects ) {
$templates = rgar( $field, 'gppa-choices-templates', array() );

if ( rgar( $templates, 'colorPicker_color' ) ) {
$choice['colorPicker_color'] = gp_populate_anything()->process_template( $field, 'colorPicker_color', $object, 'choices', $objects );
}

return $choice;
}

public function add_colorpicker_choice_template() {
?>
<script type="text/javascript">
window.gform.addFilter( 'gppa_template_rows', function ( templateRows, field, populate ) {
if ( populate !== 'choices' ) {
return templateRows;
}

templateRows.push( {
id: 'colorPicker_color',
label: '<?php echo esc_js( __( 'Color Picker', 'gp-populate-anything' ) ); ?>',
required: false,
} );

return templateRows;
} );
</script>
<?php
}
}

new GPPA_Compatibility_JetSloth_Color_Picker_Template();
Loading