: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @copyright Copyright (c) 2023, Code Atlantic LLC
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
* @deprecated 1.4 Use PUM_Fields instead.
public $field_prefix = 'settings';
public $field_name_format = '{$prefix}[{$section}][{$field}]';
public $templ_value_format = '{$prefix}{$section}.{$field}';
private static $instances = [];
public function __construct( $args = [] ) {
$sections = isset( $args['sections'] ) ? $args['sections'] : [
'title' => __( 'General', 'popup-maker' ),
$this->add_sections( $sections );
if ( ! empty( $args['fields'] ) ) {
$this->add_fields( $args['fields'] );
public static function instance( $args = [] ) {
$class = get_called_class();
$class_key = md5( $class );
if ( ! isset( self::$instances[ $class_key ] ) || ! self::$instances[ $class_key ] instanceof $class ) {
self::$instances[ $class_key ] = new $class( $args );
return self::$instances[ $class_key ];
* This function should no longer be used.
* @deprecated 1.4 Replace with add_section()
public function register_section( $id, $title, $callback = null ) {
public function add_sections( $sections ) {
foreach ( $sections as $id => $section ) {
if ( ! is_array( $section ) ) {
if ( empty( $section['id'] ) ) {
$this->add_section( $section );
public function add_section( $section ) {
$section = wp_parse_args(
$this->sections[ $section['id'] ] = $section;
public function add_field( $field = [] ) {
'object_type' => 'post_type',
'unit' => __( 'ms', 'popup-maker' ),
'button_type' => 'submit',
if ( ! $field['name'] ) {
$field['name'] = $this->get_field_name( $field );
if ( ! $field['templ_name'] ) {
$field['templ_name'] = $this->get_templ_name( $field );
$this->fields[ $field['section'] ][ $field['id'] ] = $field;
public function add_fields( $fields = [], $section = null ) {
* Switch the variables for backward compatibility with a
* select few extensions that started using the v1.3 Settings API
if ( is_string( $fields ) && is_array( $section ) ) {
foreach ( $fields as $key => $field ) {
// Either an undefined field or empty section. So lets skip it.
$first_key = key( $field );
if ( isset( $this->sections[ $key ] ) && is_array( $field[ $first_key ] ) ) {
$this->add_fields( $field, $key );
$field['section'] = $section;
if ( empty( $field['id'] ) && ! is_numeric( $key ) ) {
$this->add_field( $field );
public function get_sections() {
public function get_fields( $section = null ) {
return $this->get_all_fields();
if ( ! isset( $this->fields[ $section ] ) ) {
$non_priority_fields = [];
foreach ( $this->fields[ $section ] as $field_id => $field ) {
if ( ! isset( $field['priority'] ) || is_null( $field['priority'] ) ) {
$non_priority_fields[ $field_id ] = $field;
$priority_fields[ $field_id ] = $field;
uasort( $priority_fields, [ $this, 'sort_by_priority' ] );
$fields = $priority_fields + $non_priority_fields;
public function get_all_fields() {
foreach ( $this->fields as $section => $fields ) {
$all_fields[ $section ] = $this->get_fields( $section );
* Returns the a generated field name for given ID.
* Replaces {$prefix} with $field_prefix, {$section}
* with $section and {$field} with $field
* @return string $field_name
* @internal param $section
* @uses public $field_prefix
* @uses public $field_name_format
public function get_field_name( $field ) {
public function get_field_names( $section ) {
foreach ( $this->get_fields( $section ) as $id => $args ) {
$names[] = $this->get_field_name( $args );
public function get_templ_name( $args ) {
'general' !== $args['section'] ? ".{$args['section']}" : '',
$this->templ_value_format
function render_fields_by_section( $section = 'general', $values = [] ) {
foreach ( $this->get_fields( $section ) as $key => $args ) {
$value = isset( $values[ $args['id'] ] ) ? $values[ $args['id'] ] : null;
$this->render_field( $args, $value );
function render_fields( $values = [] ) {
foreach ( $this->get_all_fields() as $section => $fields ) {
foreach ( $fields as $id => $args ) {
$value = isset( $values[ $args['id'] ] ) ? $values[ $args['id'] ] : null;
$this->render_field( $args, $value );
public function render_field( $args = [], $value = null ) {
// If no type default to text.
$type = ! empty( $args['type'] ) ? $args['type'] : 'text';
* Check if any actions hooked to this type of field and load run those.
if ( has_action( "pum_{$type}_field" ) ) {
do_action( "pum_{$type}_field", $args, $value );
* Check if override or custom function exists and load that.
if ( function_exists( "pum_{$type}_callback" ) ) {
$function_name = "pum_{$type}_callback";
* Check if core method exists and load that.
*/ elseif ( method_exists( $this, $type . '_callback' ) ) {
$function_name = [ $this, $type . '_callback' ];
* No method exists, lets notify them the field type doesn't exist.
$function_name = [ $this, 'missing_callback' ];
* Call the determined method, passing the field args & $value to the callback.
call_user_func_array( $function_name, [ $args, $value ] );
public function render_templ_fields() {
foreach ( $this->get_all_fields() as $section => $fields ) {
foreach ( $fields as $id => $args ) {
$this->render_templ_field( $args );
public function render_templ_fields_by_section( $section = 'general' ) {
foreach ( $this->get_fields( $section ) as $key => $args ) {
$this->render_templ_field( $args );
public function render_templ_field( $args = [] ) {
// If no type default to text.
$type = ! empty( $args['type'] ) ? $args['type'] : 'text';
* Check if any actions hooked to this type of field and load run those.
if ( has_action( "pum_{$type}_templ_field" ) ) {
do_action( "pum_{$type}_templ_field", $args, $this );
* Check if override or custom function exists and load that.
if ( function_exists( "pum_{$type}_templ_callback" ) ) {
$function_name = "pum_{$type}_templ_callback";
* Check if core method exists and load that.
*/ elseif ( method_exists( $this, $type . '_templ_callback' ) ) {
$function_name = [ $this, $type . '_templ_callback' ];
* Check if the field type is hook.
*/ elseif ( 'hook' === $type ) {
$function_name = [ $this, 'hook_callback' ];
* No method exists, lets notify them the field type doesn't exist.
$function_name = [ $this, 'missing_callback' ];
* Call the determined method, passing the field args & $value to the callback.
call_user_func_array( $function_name, [ $args, $this ] );
public function field_before( $args = [] ) {
$classes = is_array( $args ) ? $this->field_classes( $args ) : ( is_string( $args ) ? $args : '' );
?><div class="<?php echo esc_attr( $classes ); ?>">