| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
/** |
| 6 |
* Render the settings page |
| 7 |
*/ |
| 8 |
class Settings { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'admin_init', [ $this, 'settings_init' ] ); |
| 15 |
add_action( 'admin_menu', [ $this, 'add_menu_item' ] ); |
| 16 |
add_action( 'admin_enqueue_scripts', [ $this, 'add_admin_scripts' ] ); |
| 17 |
add_action( 'admin_print_scripts', [ $this, 'admin_head_scripts' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize settings |
| 22 |
*/ |
| 23 |
public function settings_init() { |
| 24 |
$this->upgrade_settings(); |
| 25 |
register_setting( 'isc_options_group', 'isc_options', [ $this, 'settings_validation' ] ); |
| 26 |
new \ISC\Settings\Sections\Newsletter(); |
| 27 |
new \ISC\Settings\Sections\Plugin_Options(); |
| 28 |
new \ISC\Settings\Sections\Compatibility(); |
| 29 |
new \ISC\Settings\Sections\Caption(); |
| 30 |
new \ISC\Settings\Sections\Page_List(); |
| 31 |
new \ISC\Settings\Sections\Global_List(); |
| 32 |
new \ISC\Settings\Sections\Ai_Images(); |
| 33 |
new \ISC\Settings\Sections\Licenses(); |
| 34 |
new \ISC\Settings\Sections\Miscellaneous(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the settings page |
| 39 |
*/ |
| 40 |
public function add_menu_item() { |
| 41 |
add_options_page( |
| 42 |
__( 'Image Source Control Settings', 'image-source-control-isc' ), |
| 43 |
__( 'Image Sources', 'image-source-control-isc' ), |
| 44 |
'edit_others_posts', |
| 45 |
'isc-settings', |
| 46 |
[ $this, 'render_settings_page' ] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Render the settings page |
| 52 |
*/ |
| 53 |
public function render_settings_page() { |
| 54 |
global $wp_settings_sections; |
| 55 |
|
| 56 |
if ( ! isset( $wp_settings_sections['isc_settings_page'] ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$page = 'isc_settings_page'; |
| 61 |
$settings_section = $wp_settings_sections[ $page ]; |
| 62 |
|
| 63 |
require_once ISCPATH . '/admin/templates/settings/settings.php'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add scripts to ISC-related pages |
| 68 |
*/ |
| 69 |
public function add_admin_scripts() { |
| 70 |
$screen = get_current_screen(); |
| 71 |
|
| 72 |
if ( isset( $screen->id ) && $screen->id === 'settings_page_isc-settings' ) { |
| 73 |
Helpers::enqueue_script( 'isc_settings_script', 'admin/assets/js/settings.js' ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Display scripts in <head></head> section of the settings page. |
| 79 |
* Useful for creating js variables in the js global namespace. |
| 80 |
*/ |
| 81 |
public function admin_head_scripts() { |
| 82 |
$screen = get_current_screen(); |
| 83 |
if ( isset( $screen->id ) && $screen->id === 'settings_page_isc-settings' ) { |
| 84 |
?> |
| 85 |
<script> |
| 86 |
isc_settings = { |
| 87 |
baseurl: '<?php echo esc_url( ISCBASEURL ); ?>' |
| 88 |
}; |
| 89 |
</script> |
| 90 |
<style> |
| 91 |
#isc-settings-caption-pos-options { |
| 92 |
background: url(<?php echo esc_url( ISCBASEURL ) . 'admin/templates/settings/preview/image-for-position-preview.jpg'; ?>) no-repeat center/cover; |
| 93 |
} |
| 94 |
</style> |
| 95 |
<?php |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Manage data structure upgrading of outdated versions |
| 101 |
*/ |
| 102 |
public function upgrade_settings() { |
| 103 |
$default_options = Plugin::default_options(); |
| 104 |
|
| 105 |
/** |
| 106 |
* This function checks options in database |
| 107 |
* during the admin_init hook to handle plugin's upgrade. |
| 108 |
*/ |
| 109 |
$options = get_option( 'isc_options', $default_options ); |
| 110 |
|
| 111 |
if ( is_array( $options ) ) { |
| 112 |
// version 1.7 and higher |
| 113 |
if ( version_compare( '1.7', $options['version'], '>' ) ) { |
| 114 |
// convert old into new settings |
| 115 |
if ( isset( $options['attach_list_to_post'] ) ) { |
| 116 |
$options['display_type'][] = 'list'; |
| 117 |
} |
| 118 |
if ( isset( $options['source_on_image'] ) ) { |
| 119 |
$options['display_type'][] = 'overlay'; |
| 120 |
} |
| 121 |
} |
| 122 |
} else { |
| 123 |
// create options from default just in case the isc_option is stored with something other than an array in it. |
| 124 |
update_option( 'isc_options', $default_options ); |
| 125 |
$options = $default_options; |
| 126 |
} |
| 127 |
|
| 128 |
if ( ISCVERSION !== $options['version'] ) { |
| 129 |
$options = $options + $default_options; |
| 130 |
$options['version'] = ISCVERSION; |
| 131 |
update_option( 'isc_options', $options ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Input validation function. |
| 137 |
* |
| 138 |
* @param array $input values from the admin panel. |
| 139 |
*/ |
| 140 |
public function settings_validation( $input ) { |
| 141 |
$output = Plugin::get_options(); |
| 142 |
|
| 143 |
// the display_type option array is split between various sections, so best to validate it here |
| 144 |
if ( ! is_array( $input['display_type'] ) ) { |
| 145 |
$output['display_type'] = []; |
| 146 |
} else { |
| 147 |
$output['display_type'] = $input['display_type']; |
| 148 |
} |
| 149 |
|
| 150 |
// add activation date when the settings are saved for the first time |
| 151 |
if ( ! array_key_exists( 'activated', $output ) ) { |
| 152 |
$output['activated'] = time(); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Allow our setting section classes to validate or manipulate settings on save |
| 157 |
*/ |
| 158 |
return apply_filters( 'isc_settings_on_save_after_validation', $output, $input ); |
| 159 |
} |
| 160 |
} |
| 161 |
|