PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.11.0
Image Source Control Lite – Show Image Credits and Captions v3.11.0
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / settings.php

settings.php in Image Source Control Lite – Show Image Credits and Captions 3.11.0, at includes/settings.php

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