Skip to content

Commit

Permalink
~ Added scoping setting so that custom CSS can be optionally scoped t…
Browse files Browse the repository at this point in the history
…o the form it is associated with.
  • Loading branch information
veryspry committed Feb 2, 2024
1 parent 51fa538 commit 92ff95d
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions class-gwiz-gf-custom-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function init_auto_updater() {
'changelog_url' => 'https://raw.githubusercontent.com/gravitywiz/gf-custom-code/master/changelog.txt',
'icons' => array(
// TODO make sure this dashicon id string works correctly:
'dashicon-editor-code'
'dashicon-editor-code',
// 'svg' => 'https://raw.githubusercontent.com/gravitywiz/gf-custom-code/master/icon.svg',
),
'banners' => array(
Expand Down Expand Up @@ -293,8 +293,10 @@ public function noconflict_styles( $scripts = array() ) {
public function save_custom_code_settings( $feed_id, $form_id, $settings, $feed_addon_instance ) {
$form = GFAPI::get_form( $form_id );

$form['custom_js'] = esc_html( rgpost( 'custom_js' ) );
$form['custom_css'] = esc_html( rgpost( 'custom_css' ) );
$form['custom_js'] = esc_html( rgpost( 'custom_js' ) );

Check warning on line 296 in class-gwiz-gf-custom-code.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 21 spaces
$form['custom_css'] = esc_html( rgpost( 'custom_css' ) );

Check warning on line 297 in class-gwiz-gf-custom-code.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 1 space but found 20 spaces
// TODO: there must be a better way to do this (e.g. automatically handled by GF 🤔🤔🤔).
$form['custom_code_scope_css_to_form'] = rgpost( '_gform_setting_custom_code_scope_css_to_form' ) === '1' ? true : false;

GFAPI::update_form( $form );
}
Expand Down Expand Up @@ -391,6 +393,15 @@ public function add_custom_css( $form_string, $form ) {
$custom_css = html_entity_decode( $custom_css );
$custom_css = str_replace( 'GFFORMID', $form['id'], $custom_css );

// check explicity if not set to false as the default value is "true"
// and unset value, empty string, etc. implies that the user has not
// explicity changed this.
if ( rgar( $form, 'custom_code_scope_css_to_form' ) !== false ) {
// alternatively this could be scoped to the form element with `#gform_FORMID`
$prefix = '#gform_wrapper_' . $form['id'];
$custom_css = $this->prefix_css_selectors( $custom_css, $prefix );
}

if ( ! empty( $custom_css ) ) {
$form_string .= sprintf( '<style>%s</style>', $custom_css );
}
Expand Down Expand Up @@ -453,6 +464,13 @@ public function feed_settings_fields() {
return $this->render_custom_css_setting( $form );
},
),
array(
'name' => 'custom_code_scope_css_to_form',
'type' => 'toggle',
'label' => __( 'Scope CSS to this form only', 'gw-custom-code' ),
'tooltip' => __( 'When enabled, the custom CSS will only be applied to this form. This works by adding "#gform_wrapper_GFFORMID" before all detected selectors.', 'gw-custom-code' ),
'default_value' => true,
),
),
),
);
Expand Down Expand Up @@ -630,4 +648,11 @@ public function maybe_display_custom_js_warning() {
echo '</div>';
}
}


public function prefix_css_selectors( $css, $prefix ) {
return preg_replace_callback('/([^\r\n,{}]+)(,(?=[^}]*{)|\s*{)/', function( $matches ) use ( $prefix ) {
return $prefix . ' ' . trim( $matches[1] ) . $matches[2];
}, $css);
}
}

0 comments on commit 92ff95d

Please sign in to comment.