| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings Helper Class |
| 4 |
*/ |
| 5 |
|
| 6 |
class Wp_post_view_settings |
| 7 |
{ |
| 8 |
public static function settings_init(){ |
| 9 |
// add_action( 'admin_menu', array( 'Wp_post_view_settings', 'settings_options' ) ); |
| 10 |
// add_action( 'admin_init', array( 'Wp_post_view_settings', 'settings_init_call' ) ); |
| 11 |
|
| 12 |
add_action( 'admin_menu', array( 'Wp_post_view_settings','wppv_api_add_admin_menu' ) ); |
| 13 |
|
| 14 |
add_action( 'admin_init', array( 'Wp_post_view_settings','wppv_api_settings_init' ) ); |
| 15 |
|
| 16 |
} |
| 17 |
public static function wppv_activation_hook(){ |
| 18 |
if(empty(get_option( 'wppv_api_settings' ))){ |
| 19 |
$options = array( |
| 20 |
'wppv_api_text_field_0' => '1' , |
| 21 |
'wppv_api_text_field_1' => '1', |
| 22 |
'wppv_api_post_checkbox_1'=> array( |
| 23 |
'post' => 'post', |
| 24 |
'page' => 'page' |
| 25 |
), |
| 26 |
); |
| 27 |
update_option( 'wppv_api_settings' , $options , 'yes'); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public static function wppv_api_add_admin_menu( ) { |
| 32 |
add_options_page( 'Wp Post Views Settings', 'Wp Post Views Settings', 'manage_options', 'settings-api-page', array( 'Wp_post_view_settings','wppv_api_options_page' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
public static function wppv_api_settings_init( ) { |
| 36 |
register_setting( 'wppvPlugin', 'wppv_api_settings' ); |
| 37 |
add_settings_section( |
| 38 |
'wppv_api_wppvPlugin_section', |
| 39 |
__( 'Settings for WP Post Views', 'wppv' ),array( 'Wp_post_view_settings','wppv_api_settings_section_callback'), |
| 40 |
'wppvPlugin' |
| 41 |
); |
| 42 |
|
| 43 |
add_settings_field( |
| 44 |
'wppv_api_text_field_0', |
| 45 |
__( 'Show post views coloumn', 'wppv' ),array( 'Wp_post_view_settings','wppv_show_views_callback'), |
| 46 |
'wppvPlugin', |
| 47 |
'wppv_api_wppvPlugin_section' |
| 48 |
); |
| 49 |
add_settings_field( |
| 50 |
'wppv_api_text_field_1', |
| 51 |
__( 'Views filter on IP (If checked multiple views will not count From same IP)', 'wppv' ),array( 'Wp_post_view_settings','filter_on_ip_callback'), |
| 52 |
'wppvPlugin', |
| 53 |
'wppv_api_wppvPlugin_section' |
| 54 |
); |
| 55 |
add_settings_field( |
| 56 |
'wppv_api_post_checkbox_1', |
| 57 |
__( 'Select your custom post type', 'wppv' ),array( 'Wp_post_view_settings','select_post_type_callback'), |
| 58 |
'wppvPlugin', |
| 59 |
'wppv_api_wppvPlugin_section' |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public static function wppv_show_views_callback( ) { |
| 64 |
$options = get_option( 'wppv_api_settings' ); |
| 65 |
$checkbox_val = empty($options['wppv_api_text_field_0']) ? '' : $options['wppv_api_text_field_0'] ; |
| 66 |
?> |
| 67 |
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_0]' value="1" <?php checked( 1, $checkbox_val, true ); ?>> |
| 68 |
<?php |
| 69 |
} |
| 70 |
|
| 71 |
public static function filter_on_ip_callback( ) { |
| 72 |
$options = get_option( 'wppv_api_settings' ); |
| 73 |
$checkbox_val = empty($options['wppv_api_text_field_1']) ? '' : $options['wppv_api_text_field_1'] ; |
| 74 |
?> |
| 75 |
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_1]' value="1" <?php checked( 1, $checkbox_val, true ); ?>> |
| 76 |
<?php |
| 77 |
} |
| 78 |
|
| 79 |
public static function select_post_type_callback( ) { |
| 80 |
$options = get_option( 'wppv_api_settings' ); |
| 81 |
$checkbox_val = empty($options['wppv_api_post_checkbox_1']) ? '' : $options['wppv_api_post_checkbox_1'] ; |
| 82 |
$args = [ |
| 83 |
'public' => true, |
| 84 |
'_builtin' => false, |
| 85 |
]; |
| 86 |
$post_types = get_post_types( $args, 'objects' ); |
| 87 |
/* BUILT IN POST TYPE PAGE AND POST OPTION */ |
| 88 |
?> |
| 89 |
<label for="">Posts </label> |
| 90 |
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][post]' value="post" <?php checked( ( 'post' == @$checkbox_val['post']), true ); ?>> |
| 91 |
<label for="">Pages </label><input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][page]' value="page" <?php checked( ( 'page' == @$checkbox_val['page']), true ); ?>> |
| 92 |
<?php |
| 93 |
foreach($post_types as $post_type){ |
| 94 |
?> |
| 95 |
<label for=""><?php echo $post_type->label;?> </label> |
| 96 |
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][<?php echo $post_type->name ?>]' value="<?php echo $post_type->name ?>" <?php checked( ( $post_type->name == @$checkbox_val[$post_type->name]), true ); ?>> |
| 97 |
<?php |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static function wppv_api_settings_section_callback( ) { |
| 102 |
echo __( 'This will show one extra coloumn in Post listing page which is show the counts', 'wppv' ); |
| 103 |
} |
| 104 |
|
| 105 |
public static function wppv_api_options_page( ) { |
| 106 |
?> |
| 107 |
<form action='options.php' method='post'> |
| 108 |
<h2>Wp post View All Settings Admin Page</h2> |
| 109 |
<?php |
| 110 |
settings_fields( 'wppvPlugin' ); |
| 111 |
do_settings_sections( 'wppvPlugin' ); |
| 112 |
submit_button(); |
| 113 |
?> |
| 114 |
</form> |
| 115 |
<?php |
| 116 |
} |
| 117 |
} |
| 118 |
?> |