cf7-wc-product-settings
class Cf7_woo_products_settings {
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'init_settings' ) );
}
public function add_admin_menu() {
add_options_page(
esc_html__( 'CF7 WooCommerce Product Settings', 'cf7-woo-products' ),
esc_html__( 'CF7 / Woo Settings', 'cf7-woo-products' ),
'manage_options',
'cf7-wc-products',
array( $this, 'dd_cf7_wc_settings_callback' )
);
}
public function init_settings() {
register_setting(
'cf7_woo_plugin_settings',
'cf7_wc_products'
);
add_settings_section(
'cf7_wc_products_section',
'',
false,
'cf7_wc_products'
);
add_settings_field(
'wc_product_cats',
__( 'Choose the Product Categories', 'cf7-woo-products' ),
array( $this, 'render_wc_product_cats_field' ),
'cf7_wc_products',
'cf7_wc_products_section'
);
add_settings_field(
'choose_type',
__( 'Include or Exclude', 'cf7-woo-products' ),
array( $this, 'render_choose_type_field' ),
'cf7_wc_products',
'cf7_wc_products_section'
);
add_settings_field(
'include_select2',
__( 'Include Select 2 on Front End', 'cf7-woo-products' ),
array( $this, 'render_include_select2_field' ),
'cf7_wc_products',
'cf7_wc_products_section'
);
}
public function dd_cf7_wc_settings_callback() {
// Check required user capability
if ( !current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'cf7-woo-products' ) );
}
// Admin Page Layout
echo '<div class="wrap">' . "n";
echo ' <h1>' . get_admin_page_title() . '</h1>' . "n";
echo ' <form action="options.php" method="post">' . "n";
settings_fields( 'cf7_woo_plugin_settings' );
do_settings_sections( 'cf7_wc_products' );
submit_button();
echo ' </form>' . "n";
echo '</div>' . "n";
}
function render_wc_product_cats_field() {
// Retrieve data from the database.
$options = get_option( 'cf7_wc_products' );
// Set default value.
$value = isset( $options['wc_product_cats'] ) ? $options['wc_product_cats'] : array();
// Field output.
echo '<input type="checkbox" name="cf7_wc_products[wc_product_cats][]" class="wc_product_cats_field" value="' . esc_attr( 'Value1' ) . '" ' . ( in_array( 'Value1', $value )? 'checked="checked"' : '' ) . '> ' . __( 'Label1', 'cf7-woo-products' ) . '<br>';
echo '<input type="checkbox" name="cf7_wc_products[wc_product_cats][]" class="wc_product_cats_field" value="' . esc_attr( 'Value2' ) . '" ' . ( in_array( 'Value2', $value )? 'checked="checked"' : '' ) . '> ' . __( 'Label2', 'cf7-woo-products' ) . '<br>';
echo '<p class="description">' . __( 'Select the product categories to be available to the', 'cf7-woo-products' ) . '</p>';
}
function render_choose_type_field() {
// Retrieve data from the database.
$options = get_option( 'cf7_wc_products' );
// Set default value.
$value = isset( $options['choose_type'] ) ? $options['choose_type'] : '';
// Field output.
echo '<input type="radio" name="cf7_wc_products[choose_type]" class="choose_type_field" value="' . esc_attr( 'include' ) . '" ' . checked( $value, 'include', false ) . '> ' . __( 'Include', 'cf7-woo-products' ) . '<br>';
echo '<input type="radio" name="cf7_wc_products[choose_type]" class="choose_type_field" value="' . esc_attr( 'exclude' ) . '" ' . checked( $value, 'exclude', false ) . '> ' . __( 'Exclude', 'cf7-woo-products' ) . '<br>';
echo '<p class="description">' . __( 'Choose how the filter should operate. Do you want to include only the checked categories, or exclude the checked categories.', 'cf7-woo-products' ) . '</p>';
}
function render_include_select2_field() {
// Retrieve data from the database.
$options = get_option( 'cf7_wc_products' );
// Set default value.
$value = isset( $options['include_select2'] ) ? $options['include_select2'] : '';
// Field output.
echo '<input type="radio" name="cf7_wc_products[include_select2]" class="include_select2_field" value="' . esc_attr( 'yes' ) . '" ' . checked( $value, 'yes', false ) . '> ' . __( 'Yes', 'cf7-woo-products' ) . '<br>';
echo '<input type="radio" name="cf7_wc_products[include_select2]" class="include_select2_field" value="' . esc_attr( 'no' ) . '" ' . checked( $value, 'no', false ) . '> ' . __( 'No', 'cf7-woo-products' ) . '<br>';
echo '<p class="description">' . __( 'Choosing yes will include Select2 jQuery on Frontend', 'cf7-woo-products' ) . '</p>';
}
}
new Cf7_woo_products_settings;