Skip to content
Merged
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
53 changes: 53 additions & 0 deletions classes/controllers/FrmStylesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class FrmStylesController {
*/
private static $message;

/**
* Cache of the active style object keyed by form ID.
*
* @since x.x
*
* @var array
*/
private static $active_style = array();

/**
* @return void
*/
Expand Down Expand Up @@ -1273,6 +1282,50 @@ public static function get_form_style( $form = 'default' ) {
return $frm_style->get_one();
}

/**
* Get the active style object for a field's form.
*
* @since x.x
*
* @param array|int $field The 'field' array.
*
* @return object
*/
public static function get_active_style( $field ) {
if ( ! is_array( $field ) ) {
return new stdClass();
}

$form_id = $field['parent_form_id'] ?? $field['form_id'];

if ( isset( self::$active_style[ $form_id ] ) ) {
return self::$active_style[ $form_id ];
}

$active_style = self::get_form_style( $form_id );

if ( ! is_object( $active_style ) ) {
$active_style = new stdClass();
}
Comment on lines +1307 to +1309
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Potential undefined property access in downstream code.

When get_form_style returns null, this code converts it to an empty stdClass and caches it. Later, FrmFieldType::get_align_class_from_style_settings() (line 1109) accesses $active_style->post_content[$key] on this object. Since the empty stdClass has no post_content property, this will trigger a PHP notice: "Undefined property: stdClass::$post_content" before the null coalescing operator can suppress it.

🛡️ Recommended fix to ensure post_content exists

Option 1 (fix here in FrmStylesController):

 		if ( ! is_object( $active_style ) ) {
-			$active_style = new stdClass();
+			$active_style              = new stdClass();
+			$active_style->post_content = array();
 		}

Option 2 (fix in FrmFieldType at line 1109):

-		return $active_style->post_content[ $key ] ?? '';
+		return ( $active_style->post_content ?? array() )[ $key ] ?? '';

Either approach prevents the undefined property notice.

📝 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
if ( ! is_object( $active_style ) ) {
$active_style = new stdClass();
}
if ( ! is_object( $active_style ) ) {
$active_style = new stdClass();
$active_style->post_content = array();
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@classes/controllers/FrmStylesController.php` around lines 1307 - 1309, When
get_form_style() returns null we currently replace it with new stdClass(), but
downstream FrmFieldType::get_align_class_from_style_settings() expects
$active_style->post_content to be an array and accesses
$active_style->post_content[$key], causing an undefined property notice; update
the fallback in FrmStylesController around the $active_style assignment so that
when creating the stdClass you also initialize $active_style->post_content = []
(an empty array) before caching/returning it, ensuring the property exists and
behaves as expected by get_align_class_from_style_settings().


self::$active_style[ $form_id ] = $active_style;

return self::$active_style[ $form_id ];
}

/**
* Get the style setting key that stores the alignment for a field type.
*
* @since x.x
*
* @param string $field_type
*
* @return string
*/
public static function get_align_key_for_style_settings( $field_type ) {
return 'checkbox' === $field_type ? 'check_align' : 'radio_align';
}

/**
* @param string $class
* @param string $style
Expand Down
22 changes: 21 additions & 1 deletion classes/models/fields/FrmFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,13 +1082,33 @@ public function get_container_class() {
return '';
}

$align = FrmField::get_option( $this->field, 'align' );
$align = FrmAppHelper::pro_is_installed() ? FrmField::get_option( $this->field, 'align' ) : '';

if ( ! $align ) {
$align = $this->get_align_class_from_style_settings();
}

$this->prepare_align_class( $align );

return $align ? ' ' . $align : '';
}

/**
* Get the alignment value from the active style settings.
*
* Used as the fallback when the field has no specific alignment option set.
*
* @since x.x
*
* @return string
*/
protected function get_align_class_from_style_settings() {
$key = FrmStylesController::get_align_key_for_style_settings( FrmField::get_field_type( $this->field ) );
$active_style = FrmStylesController::get_active_style( $this->field );

return $active_style->post_content[ $key ] ?? '';
}

/**
* @since 4.0
*
Expand Down
Loading