| 1 |
<?php |
| 2 |
/** |
| 3 |
* Flex Posts Options |
| 4 |
* |
| 5 |
* @package Flex Posts |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Flex_Posts; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Flex Posts options class |
| 16 |
*/ |
| 17 |
class Options { |
| 18 |
|
| 19 |
/** |
| 20 |
* Holds the values to be used in the fields callbacks |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $options; |
| 25 |
|
| 26 |
/** |
| 27 |
* Options page slug |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $page = 'flex-posts'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Options group name |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $group = 'flex_posts_group'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Option name |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
private $option_name = 'flex_posts'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Sets options page and menu. |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
add_action( 'admin_init', array( $this, 'settings_init' ) ); |
| 52 |
add_action( 'admin_menu', array( $this, 'add_menu_page' ), 11 ); |
| 53 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Enqueue scripts & styles |
| 58 |
*/ |
| 59 |
public function enqueue() { |
| 60 |
$screen = get_current_screen(); |
| 61 |
if ( 'settings_page_flex-posts' !== $screen->id ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
wp_enqueue_style( |
| 66 |
'flex-posts-options', |
| 67 |
FLEX_POSTS_URL . 'admin/css/options.css', |
| 68 |
array(), |
| 69 |
FLEX_POSTS_VERSION |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add options page |
| 75 |
*/ |
| 76 |
public function add_menu_page() { |
| 77 |
add_options_page( |
| 78 |
__( 'Flex Posts', 'flex-posts' ), |
| 79 |
__( 'Flex Posts', 'flex-posts' ), |
| 80 |
'manage_options', |
| 81 |
$this->page, |
| 82 |
array( $this, 'settings_page' ) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Add settings |
| 88 |
*/ |
| 89 |
public function settings_init() { |
| 90 |
register_setting( |
| 91 |
$this->group, |
| 92 |
$this->option_name, |
| 93 |
array( |
| 94 |
$this, |
| 95 |
'sanitize', |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
add_settings_section( 'flex_posts_section_1', __( 'Settings', 'flex-posts' ), '', $this->page ); |
| 100 |
|
| 101 |
add_settings_field( |
| 102 |
'disable_css', |
| 103 |
__( 'Disable CSS', 'flex-posts' ), |
| 104 |
array( $this, 'field_disable_css' ), |
| 105 |
$this->page, |
| 106 |
'flex_posts_section_1' |
| 107 |
); |
| 108 |
|
| 109 |
add_settings_field( |
| 110 |
'disable_image_size', |
| 111 |
__( 'Disable Image Size', 'flex-posts' ), |
| 112 |
array( $this, 'field_disable_image_size' ), |
| 113 |
$this->page, |
| 114 |
'flex_posts_section_1' |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Display field |
| 120 |
*/ |
| 121 |
public function field_disable_css() { |
| 122 |
$val = empty( $this->options['disable_css'] ) ? false : true; |
| 123 |
?> |
| 124 |
<label> |
| 125 |
<?php |
| 126 |
flex_posts_input( |
| 127 |
array( |
| 128 |
'type' => 'checkbox', |
| 129 |
'name' => $this->option_name . '[disable_css]', |
| 130 |
'class' => 'checkbox', |
| 131 |
'value' => 1, |
| 132 |
'checked' => $val, |
| 133 |
) |
| 134 |
); |
| 135 |
?> |
| 136 |
<span><?php esc_html_e( 'Disable CSS', 'flex-posts' ); ?></span> |
| 137 |
<p class="description"><?php esc_html_e( 'This plugin uses a single CSS file (flex-posts.css). If this option is on, the CSS file will not be loaded.', 'flex-posts' ); ?></p> |
| 138 |
</label> |
| 139 |
<?php |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Display field |
| 144 |
*/ |
| 145 |
public function field_disable_image_size() { |
| 146 |
$val = empty( $this->options['disable_image_size'] ) ? false : true; |
| 147 |
?> |
| 148 |
<label> |
| 149 |
<?php |
| 150 |
flex_posts_input( |
| 151 |
array( |
| 152 |
'type' => 'checkbox', |
| 153 |
'name' => $this->option_name . '[disable_image_size]', |
| 154 |
'class' => 'checkbox', |
| 155 |
'value' => 1, |
| 156 |
'checked' => $val, |
| 157 |
) |
| 158 |
); |
| 159 |
?> |
| 160 |
<span><?php esc_html_e( 'Disable Additional Image Size', 'flex-posts' ); ?></span> |
| 161 |
<p class="description"><?php esc_html_e( 'This plugin adds a new custom image size (400×250 cropped). If this option is on, this image size will not be generated.', 'flex-posts' ); ?></p> |
| 162 |
</label> |
| 163 |
<?php |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Sanitize input data |
| 168 |
* |
| 169 |
* @param array $input Input data. |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function sanitize( $input ) { |
| 173 |
$new_input['disable_css'] = empty( $input['disable_css'] ) ? false : true; |
| 174 |
$new_input['disable_image_size'] = empty( $input['disable_image_size'] ) ? false : true; |
| 175 |
return $new_input; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Display settings page |
| 180 |
*/ |
| 181 |
public function settings_page() { |
| 182 |
$this->options = get_option( $this->option_name ); |
| 183 |
?> |
| 184 |
<div class="wrap"> |
| 185 |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 186 |
<form action="options.php" method="post"> |
| 187 |
<?php |
| 188 |
settings_fields( $this->group ); |
| 189 |
do_settings_sections( $this->page ); |
| 190 |
submit_button(); |
| 191 |
?> |
| 192 |
</form> |
| 193 |
</div> |
| 194 |
<?php |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
new Options(); |
| 199 |
|