HTML = ''; $this->JS = ''; $this->type = 'text'; $this->plainHTML = false; $this->useParagraph = true; $this->helpText = ''; $this->id = ''; $this->name = ''; $this->value = ''; $this->label = ''; $this->className = 'input widefat'; $this->style = ''; $this->required = ''; $this->options = null; $this->optionName = ''; $this->optionIdentifier = ''; $this->values = ''; if ( isset( $args ) && is_array( $args ) ) { if ( isset( $args['type'] ) && ( $args['type'] != '' ) ) { $this->type = $args['type']; } if ( isset( $args['plainHTML'] ) && ( $args['plainHTML'] != '' ) ) { $this->plainHTML = $args['plainHTML']; } if ( isset( $args['useParagraph'] ) && ( $args['useParagraph'] != '' ) ) { $this->useParagraph = $args['useParagraph']; } if ( isset( $args['helpText'] ) && ( $args['helpText'] != '' ) ) { $this->helpText = $args['helpText']; } if ( isset( $args['id'] ) && ( $args['id'] != '' ) ) { $this->id = $args['id']; } if ( isset( $args['name'] ) && ( $args['name'] != '' ) ) { $this->name = $args['name']; } if ( isset( $args['value'] ) && ( $args['value'] != '' ) ) { $this->value = $args['value']; } if ( isset( $args['label'] ) && ( $args['label'] != '' ) ) { $this->label = $args['label']; } if ( isset( $args['className'] ) && ( $args['className'] != '' ) ) { $this->className = $args['className']; } if ( isset( $args['style'] ) && ( $args['style'] != '' ) ) { $this->style = $args['style']; } if ( isset( $args['required'] ) && ( $args['required'] != '' ) ) { $this->required = $args['required']; } if ( isset( $args['options'] ) && is_array( $args['options'] ) ) { $this->options = $args['options']; } if ( isset( $args['optionIdentifier'] ) && ( $args['optionIdentifier'] != '' ) ) { $this->optionIdentifier = $args['optionIdentifier']; } if ( isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $this->optionName = $args['optionName']; } if ( isset( $args['values'] ) && ( $args['values'] != '' ) ) { $this->values = $args['values']; } if ( isset( $args['optionIdentifier'] ) && ( $args['optionIdentifier'] != '' ) && isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $this->id = str_replace( [ '[', ']' ], [ '_', '' ], $args['optionIdentifier'] ) . '_' . $args['optionName']; $this->name = $args['optionIdentifier'] . '[' . $args['optionName'] . ']'; } elseif ( isset( $this->optionIdentifier ) && ( $this->optionIdentifier != '' ) && isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $this->id = str_replace( [ '[', ']' ], [ '_', '' ], $this->optionIdentifier ) . '_' . $args['optionName']; $this->name = $this->optionIdentifier . '[' . $args['optionName'] . ']'; } elseif ( isset( $args['optionIdentifier'] ) && ( $args['optionIdentifier'] != '' ) && isset( $this->optionName ) && ( $this->optionName != '' ) ) { $this->id = str_replace( [ '[', ']' ], [ '_', '' ], $args['optionIdentifier'] ) . '_' . $this->optionName; $this->name = $args['optionIdentifier'] . '[' . $this->optionName . ']'; } elseif ( isset( $this->optionIdentifier ) && ( $this->optionIdentifier != '' ) && isset( $this->optionName ) && ( $this->optionName != '' ) ) { $this->id = str_replace( [ '[', ']' ], [ '_', '' ], $this->optionIdentifier ) . '_' . $this->optionName; $this->name = $this->optionIdentifier . '[' . $this->optionName . ']'; } if ( isset( $args['values'] ) && is_array( $args['values'] ) ) { if ( isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $this->value = $args['values'][ $args['optionName'] ]; } elseif ( isset( $this->optionName ) && ( $this->optionName != '' ) ) { $this->value = $args['values'][ $this->optionName ]; } } elseif ( isset( $this->values ) && is_array( $this->values ) ) { if ( isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $this->value = $this->values[ $args['optionName'] ]; } elseif ( isset( $this->optionName ) && ( $this->optionName != '' ) ) { $this->value = $this->values[ $this->optionName ]; } } } } private function sync_args( $args ) { $syncedArgs = []; if ( is_array( $args ) ) { $syncedArgs = $args; } if ( isset( $args['type'] ) && ( $args['type'] != '' ) ) { $syncedArgs['type'] = $args['type']; } else { $syncedArgs['type'] = $this->type; } if ( isset( $args['plainHTML'] ) && is_bool( $args['plainHTML'] ) ) { $syncedArgs['plainHTML'] = $args['plainHTML']; } else { $syncedArgs['plainHTML'] = $this->plainHTML; } if ( isset( $args['useParagraph'] ) && is_bool( $args['useParagraph'] ) ) { $syncedArgs['useParagraph'] = $args['useParagraph']; } else { $syncedArgs['useParagraph'] = $this->useParagraph; } if ( isset( $args['helpText'] ) && ( $args['helpText'] != '' ) ) { $syncedArgs['helpText'] = $args['helpText']; } else { $syncedArgs['helpText'] = $this->helpText; } if ( isset( $args['id'] ) && ( $args['id'] != '' ) ) { $syncedArgs['id'] = $args['id']; } else { $syncedArgs['id'] = $this->id; } if ( isset( $args['name'] ) && ( $args['name'] != '' ) ) { $syncedArgs['name'] = $args['name']; } else { $syncedArgs['name'] = $this->name; } if ( isset( $args['label'] ) && ( $args['label'] != '' ) ) { $syncedArgs['label'] = $args['label']; } else { $syncedArgs['label'] = $this->label; } if ( isset( $args['className'] ) && ( $args['className'] != '' ) ) { $syncedArgs['className'] = $args['className']; } else { $syncedArgs['className'] = $this->className; } if ( isset( $args['style'] ) && ( $args['style'] != '' ) ) { $syncedArgs['style'] = $args['style']; } else { $syncedArgs['style'] = $this->style; } if ( isset( $args['required'] ) && ( $args['required'] != '' ) ) { $syncedArgs['required'] = $args['required']; } else { $syncedArgs['required'] = $this->required; } if ( isset( $args['options'] ) && is_array( $args['options'] ) ) { $syncedArgs['options'] = $args['options']; } else { $syncedArgs['options'] = $this->options; } if ( isset( $args['optionIdentifier'] ) && ( $args['optionIdentifier'] != '' ) && isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $syncedArgs['id'] = str_replace( [ '[', ']' ], [ '_', '' ], $args['optionIdentifier'] ) . '_' . $args['optionName']; $syncedArgs['name'] = $args['optionIdentifier'] . '[' . $args['optionName'] . ']'; } elseif ( isset( $this->optionIdentifier ) && ( $this->optionIdentifier != '' ) && isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $syncedArgs['id'] = str_replace( [ '[', ']' ], [ '_', '' ], $this->optionIdentifier ) . '_' . $args['optionName']; $syncedArgs['name'] = $this->optionIdentifier . '[' . $args['optionName'] . ']'; } elseif ( isset( $args['optionIdentifier'] ) && ( $args['optionIdentifier'] != '' ) && isset( $this->optionName ) && ( $this->optionName != '' ) ) { $syncedArgs['id'] = str_replace( [ '[', ']' ], [ '_', '' ], $args['optionIdentifier'] ) . '_' . $this->optionName; $syncedArgs['name'] = $args['optionIdentifier'] . '[' . $this->optionName . ']'; } elseif ( isset( $this->optionIdentifier ) && ( $this->optionIdentifier != '' ) && isset( $this->optionName ) && ( $this->optionName != '' ) ) { $syncedArgs['id'] = str_replace( [ '[', ']' ], [ '_', '' ], $this->optionIdentifier ) . '_' . $this->optionName; $syncedArgs['name'] = $this->optionIdentifier . '[' . $this->optionName . ']'; } if ( isset( $args['value'] ) && ( $args['value'] != '' ) ) { $syncedArgs['value'] = $args['value']; } elseif ( isset( $args['values'] ) && is_array( $args['values'] ) ) { if ( isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $syncedArgs['value'] = $args['values'][ $args['optionName'] ]; } elseif ( isset( $this->optionName ) && ( $this->optionName != '' ) ) { $syncedArgs['value'] = $args['values'][ $this->optionName ]; } } elseif ( isset( $this->values ) && is_array( $this->values ) ) { if ( isset( $args['optionName'] ) && ( $args['optionName'] != '' ) ) { $syncedArgs['value'] = $this->values[ $args['optionName'] ]; } elseif ( isset( $this->optionName ) && ( $this->optionName != '' ) ) { $syncedArgs['value'] = $this->values[ $this->optionName ]; } } else { $syncedArgs['value'] = $this->value; } return $syncedArgs; } public function add_control( $args = null ) { $control = $this->get_control( $args, true ); $this->HTML .= $control['HTML']; $this->JS .= $control['JS']; } public function get_control( $args = null, $ignorePlainHTML = false ) { $HTML = ''; $JS = ''; $args = $this->sync_args( $args ); switch ( $args['type'] ) { case 'hidden': $HTML .= ''; break; case 'text': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'number': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'password': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'textarea': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'checkbox': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( isset( $args['value'] ) && ( filter_var( $args['value'], FILTER_VALIDATE_BOOLEAN ) ) ) { $HTML .= ''; } else { $HTML .= ''; } if ( $args['label'] != '' ) { $HTML .= ' '; } if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'radio': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( isset( $args['value'] ) && ( filter_var( $args['value'], FILTER_VALIDATE_BOOLEAN ) ) ) { $HTML .= ''; } else { $HTML .= ''; } if ( $args['label'] != '' ) { $HTML .= ' '; } if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'select': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'choosen-multiselect': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").chosen({ width: "100%" }).on("change", function(evt, params) { jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '_chosen .search-field input").click(); }).trigger("chosen:open");'; break; case 'radio-group': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } if ( is_array( $args['options'] ) ) { $index = 1; foreach ( $args['options'] as $option ) { $HTML .= ''; if ( $option['text'] != '' ) { $HTML .= ' 
'; } ++$index; } } if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'ipCheckbox': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( isset( $args['value'] ) && ( filter_var( $args['value'], FILTER_VALIDATE_BOOLEAN ) ) ) { $HTML .= ''; } else { $HTML .= ''; } if ( $args['label'] != '' ) { $HTML .= ' '; } if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").ipCheckbox();'; break; case 'minicolors': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").minicolors();'; break; case 'textarea-wysiwyg': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").jqte();'; break; case 'checkbox-button': if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( isset( $args['value'] ) && ( filter_var( $args['value'], FILTER_VALIDATE_BOOLEAN ) ) ) { $HTML .= ''; } else { $HTML .= ''; } if ( $args['label'] != '' ) { $HTML .= ''; } if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").button({ create: function(event, ui) { jQuery(this).button("option", "label", (jQuery(this).is(":checked")?"' . ( ( $args['checkedLabel'] != '' ) ? $args['checkedLabel'] : '' ) . '":"' . ( ( $args['uncheckedLabel'] != '' ) ? $args['uncheckedLabel'] : '' ) . '")) }}).change(function () { jQuery(this).button("option", "label", (jQuery(this).is(":checked")?"' . ( ( $args['checkedLabel'] != '' ) ? $args['checkedLabel'] : '' ) . '":"' . ( ( $args['uncheckedLabel'] != '' ) ? $args['uncheckedLabel'] : '' ) . '")); });'; break; case 'pages-select': $this->load_data( 'pages' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'pages-chosen-multiselect': $this->load_data( 'pages' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").chosen({ width: "100%" }).on("change", function(evt, params) { jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '_chosen .search-field input").click(); }).trigger("chosen:open");'; break; case 'posts-select': $this->load_data( 'posts' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'posts-chosen-multiselect': $this->load_data( 'posts' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").chosen({ width: "100%" }).on("change", function(evt, params) { jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '_chosen .search-field input").click(); }).trigger("chosen:open");'; break; case 'categories-select': $this->load_data( 'categories' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } break; case 'categories-chosen-multiselect': $this->load_data( 'categories' ); if ( $args['useParagraph'] ) { $HTML .= '

'; } if ( $args['label'] != '' ) { $HTML .= '
'; } $HTML .= ''; if ( $args['helpText'] != '' ) { $HTML .= '' . $args['helpText'] . ''; } if ( $args['useParagraph'] ) { $HTML .= '

'; } $JS .= 'jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '").chosen({ width: "100%" }).on("change", function(evt, params) { jQuery("#' . ( ( $args['id'] != '' ) ? $args['id'] : '' ) . '_chosen .search-field input").click(); }).trigger("chosen:open");'; break; } if ( $args['plainHTML'] && ! $ignorePlainHTML ) { return $HTML; } else { return [ 'HTML' => $HTML, 'JS' => $JS, ]; } } private function load_data( $type ) { switch ( $type ) { case 'pages': if ( ! is_array( $this->pages ) ) { $pages = get_pages( 'numberposts=100' ); if ( isset( $pages ) && is_array( $pages ) ) { $this->pages = []; foreach ( $pages as $page ) { $this->pages[] = [ 'text' => ( ( $page->post_title != '' ) ? $page->post_title : 'Untitled Page (' . $page->ID . ')' ), 'value' => $page->ID, ]; } } } break; case 'posts': if ( ! is_array( $this->posts ) ) { $posts = get_posts( 'numberposts=100' ); if ( isset( $posts ) && is_array( $posts ) ) { $this->posts = []; foreach ( $posts as $post ) { $this->posts[] = [ 'text' => $post->post_title, 'value' => $post->ID, ]; } } } break; case 'categories': if ( ! is_array( $this->categories ) ) { $categories = get_categories( 'number=200&hide_empty=0' ); if ( isset( $categories ) && is_array( $categories ) ) { $this->categories = []; foreach ( $categories as $category ) { $this->categories[] = [ 'text' => $category->name, 'value' => $category->term_id, ]; } } } break; } } public function create_section( $sectionTitle = '', $includeBlock = true ) { $wrapperHTML = '
'; if ( $sectionTitle != '' ) { $wrapperHTML .= ''; } if ( $includeBlock ) { $wrapperHTML .= $this->create_block(); } $wrapperHTML .= $this->HTML; $wrapperHTML .= '
'; $this->HTML = $wrapperHTML; } public function create_block() { $this->HTML = '
' . $this->HTML . '
'; } public function set_HTML( $HTML ) { $this->HTML = $HTML; } public function clear_controls( $clearHTML = true, $clearJS = false ) { if ( $clearHTML ) { $this->HTML = ''; } if ( $clearJS ) { $this->JS = ''; } } public static function enqueue_assets( $path, $version = '1' ) { wp_register_style( 'smartlogix-controls-css', $path . '/css/controls.css', [], $version ); wp_enqueue_style( 'smartlogix-controls-css' ); wp_register_script( 'smartlogix-controls-js', $path . '/js/controls.js', [ 'jquery', 'jquery-ui-core' ], $version, true ); wp_enqueue_script( 'smartlogix-controls-js' ); } }