1+ <?php
2+ class RedirectPage_Settings_Page {
3+
4+ public function __construct () {
5+ add_action ( 'admin_menu ' , array ( $ this , 'art_create_settings ' ) );
6+ add_action ( 'admin_init ' , array ( $ this , 'art_setup_sections ' ) );
7+ add_action ( 'admin_init ' , array ( $ this , 'art_setup_fields ' ) );
8+ add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'art_admin_styles ' ) );
9+ }
10+
11+ function art_admin_styles () {
12+ $ currentScreen = get_current_screen ();
13+ if ( $ currentScreen ->id === "toplevel_page_RedirectPageOptions " ) {
14+ wp_enqueue_style ( 'settings-style ' , plugins_url ( '/assets/css/settings.css ' , __FILE__ ));
15+ }
16+ }
17+
18+ public function art_create_settings () {
19+ $ page_title = __ ( 'Redirect Page Options ' , 'redirect-artvelog ' );
20+ $ menu_title = __ ( 'Redirect Page Options ' , 'redirect-artvelog ' );
21+ $ capability = 'manage_options ' ;
22+ $ slug = 'RedirectPageOptions ' ;
23+ $ callback = array ($ this , 'art_settings_content ' );
24+ $ icon = 'dashicons-smiley ' ;
25+ $ position = 80 ;
26+ add_menu_page ($ page_title , $ menu_title , $ capability , $ slug , $ callback , $ icon , $ position );
27+
28+ }
29+
30+ public function art_settings_content () { ?>
31+ <div class="wrap">
32+ <h1>Redirect Page Options</h1>
33+ <p class="p_description">You can edit the custom redirect page as you wish.</p>
34+ <?php settings_errors (); ?>
35+ <form method="POST" action="options.php">
36+ <?php
37+ submit_button ();
38+ settings_fields ( 'RedirectPageOptions ' );
39+ do_settings_sections ( 'RedirectPageOptions ' );
40+ ?>
41+ <hr>
42+ <?php
43+ _e ( '<h4>Other Options:</h4>
44+ If you wish, you can print the url address of the site to be redirected in your redirect page with this shortcode: <kbd>[rp_redirect_url]</kbd> <br>
45+ If you do not want your link to be redirected to the "redirect page", you can use this class. <kbd>not-redirect</kbd> <br> ' , 'redirect-artvelog ' );
46+
47+ submit_button ();
48+ ?>
49+ </form>
50+ <div class="credit-footer">
51+ <h3>Custom Redirect Page</h3> <p>© <a href="https://creative.artvelog.com">Plugin by Artvelog</a></p><h4><a href="https://github.com/artvelog/Custom-Redirect-Page">Github</a></h4>
52+ </div>
53+ </div> <?php
54+ }
55+
56+ public function art_setup_sections () {
57+ add_settings_section ( 'RedirectPageOptions_section ' , '' , array (), 'RedirectPageOptions ' );
58+ }
59+
60+
61+ public function art_setup_fields () {
62+
63+ $ args = array (
64+ 'post_type ' => 'page ' ,
65+ 'post_status ' => array ( 'publish ' , 'private ' ),
66+ );
67+ $ pages = get_pages ( $ args );
68+ $ options = array ();
69+ foreach ( $ pages as $ page ) {
70+ $ options [ $ page ->ID ] = $ page ->post_title ;
71+ }
72+
73+ $ fields = array (
74+ array (
75+ 'section ' => 'RedirectPageOptions_section ' ,
76+ 'label ' => __ ( 'Custom Redirect Page ' , 'redirect-artvelog ' ),
77+ 'placeholder ' => 'Select Custom Page ' ,
78+ 'id ' => 'custom_page_id ' ,
79+ 'desc ' => __ ( 'Please select the page whose content will be displayed on the redirect page. <b>Using a custom page would be more beneficial.</b> <br> <hr> Page ID Shortcode: <kbd>[rp_page_ID]</kbd> ' , 'redirect-artvelog ' ),
80+ 'type ' => 'select ' ,
81+ 'options ' => $ options
82+ ),
83+
84+ array (
85+ 'section ' => 'RedirectPageOptions_section ' ,
86+ 'label ' => __ ( 'Page Builder ' , 'redirect-artvelog ' ),
87+ 'placeholder ' => __ ( 'Select Page Builder ' , 'redirect-artvelog ' ),
88+ 'id ' => 'custom_page_builder ' ,
89+ 'desc ' => __ ( 'Select the plugin you used to create your page. <b class="text_red">Make sure you choose the correct one. Otherwise, your page may not display correctly.</b> ' , 'redirect-artvelog ' ),
90+ 'type ' => 'select ' ,
91+ 'options ' => array (
92+ 'elementor ' => 'Elementor ' ,
93+ 'beaver_builder ' => 'Beaver Builder ' ,
94+ 'gutenberg ' => 'Gutenberg ' ,
95+ 'visual_composer ' => 'Visual Composer ' ,
96+ 'siteorigin ' => 'SiteOrigin ' ,
97+ ),
98+ 'default ' => 'gutenberg ' ,
99+ ),
100+
101+ array (
102+ 'section ' => 'RedirectPageOptions_section ' ,
103+ 'label ' => __ ( 'Custom Redirect Page Title ' , 'redirect-artvelog ' ),
104+ 'placeholder ' => __ ( 'Title... ' , 'redirect-artvelog ' ),
105+ 'id ' => 'redirect_page_title ' ,
106+ 'desc ' => __ ( 'Enter the title of the redirect page. <br> <hr> Page Title Shortcode: <kbd>[rp_title]</kbd> ' , 'redirect-artvelog ' ),
107+ 'type ' => 'text ' ,
108+ 'default ' => 'My Custom Redirect Page ' ,
109+ 'require ' => true ,
110+ ),
111+
112+ array (
113+ 'section ' => 'RedirectPageOptions_section ' ,
114+ 'label ' => __ ( 'Rewrite Page Slug ' , 'redirect-artvelog ' ),
115+ 'placeholder ' => __ ( 'Rewrite Page Slug... ' , 'redirect-artvelog ' ),
116+ 'id ' => 'redirect_rewrite ' ,
117+ 'desc ' => __ ( 'Determine the extension of the redirect page. <b>By default, "redirect" is used.</b> <b class="text_red">After changing it, you must save the changes from the Permalinks section.</b> ' , 'redirect-artvelog ' ),
118+ 'type ' => 'text ' ,
119+ 'default ' => 'redirect ' ,
120+ 'require ' => true ,
121+ ),
122+
123+ array (
124+ 'section ' => 'RedirectPageOptions_section ' ,
125+ 'label ' => __ ( 'Delay Time ' , 'redirect-artvelog ' ),
126+ 'placeholder ' => __ ( 'Delay Time... ' , 'redirect-artvelog ' ),
127+ 'id ' => 'delay_time ' ,
128+ 'desc ' => __ ( 'Determine the delay time. <br> <hr> Delay Time Shortcode: <kbd>[rp_delay_time]</kbd> ' , 'redirect-artvelog ' ),
129+ 'type ' => 'number ' ,
130+ 'default ' => '5 ' ,
131+ 'require ' => true ,
132+ ),
133+
134+ array (
135+ 'section ' => 'RedirectPageOptions_section ' ,
136+ 'label ' => __ ( 'Usable Html Tag ' , 'redirect-artvelog ' ),
137+ 'placeholder ' => __ ( 'Usable Html Tag... ' , 'redirect-artvelog ' ),
138+ 'id ' => 'select_link_tag ' ,
139+ 'desc ' => __ ( 'Specify the class or ID of the "a" tag to be redirected. If you want to use all links directly, just write "a". <b class="text_red">(Internal links will not be redirected!)</b> ' , 'redirect-artvelog ' ),
140+ 'type ' => 'text ' ,
141+ 'default ' => 'a ' ,
142+ 'require ' => true ,
143+ ),
144+
145+ array (
146+ 'section ' => 'RedirectPageOptions_section ' ,
147+ 'label ' => __ ( 'Reference Link or Text ' , 'redirect-artvelog ' ),
148+ 'placeholder ' => __ ( 'Reference Link or Text... ' , 'redirect-artvelog ' ),
149+ 'id ' => 'redirect_ref ' ,
150+ 'desc ' => __ ( 'Enter the reference text or link. <b>You can leave it blank.</b> ' , 'redirect-artvelog ' ),
151+ 'type ' => 'text ' ,
152+ 'default ' => site_url (),
153+ ),
154+
155+ array (
156+ 'section ' => 'RedirectPageOptions_section ' ,
157+ 'label ' => __ ( 'Background Color ' , 'redirect-artvelog ' ),
158+ 'placeholder ' => '#ffff ' ,
159+ 'id ' => 'redirect_page_bg_color ' ,
160+ 'desc ' => __ ( 'You can set the background color. ' , 'redirect-artvelog ' ),
161+ 'type ' => 'color ' ,
162+ 'default ' => '#ffffff ' ,
163+ ),
164+
165+ array (
166+ 'section ' => 'RedirectPageOptions_section ' ,
167+ 'label ' => __ ( 'Redierect Page Custom Style ' , 'redirect-artvelog ' ),
168+ 'placeholder ' => __ ( 'CSS ' , 'redirect-artvelog ' ),
169+ 'id ' => 'redirectpage_custom_styles ' ,
170+ 'desc ' => __ ( 'You can add custom styles to the redirect page. ' , 'redirect-artvelog ' ),
171+ 'type ' => 'textarea ' ,
172+ )
173+ );
174+
175+ // Fields label callback function
176+ function label_cb ( $ field ) {
177+ $ _value = '' ;
178+ if ($ field ['require ' ] == true ){
179+ $ _value = $ field ['label ' ] . '<span class="require_field">*</span> ' ;
180+ }
181+ else {
182+ $ _value = $ field ['label ' ];
183+ }
184+ return $ _value ;
185+ }
186+
187+ // Fields default value function
188+ function default_value ($ field ){
189+ if ( isset ($ field ['default ' ]) ) {
190+ if (!empty ($ field ['default ' ])){
191+ if (get_option ( $ field ['id ' ] ) === false ){
192+ update_option ( $ field ['id ' ], $ field ['default ' ] );
193+ }
194+ }
195+ }
196+ }
197+
198+ foreach ( $ fields as $ field ){
199+ add_settings_field ( $ field ['id ' ], label_cb ( $ field ) , array ( $ this , 'field_cb ' ), 'RedirectPageOptions ' , $ field ['section ' ], $ field );
200+ register_setting ( 'RedirectPageOptions ' , $ field ['id ' ], function ($ value ) use ($ field ) {
201+ return validate_field ($ value , $ field );
202+ });
203+ }
204+
205+ // test
206+ //delete_option('');
207+
208+
209+ function validate_field ($ value , $ input ){
210+ /*$value = default_value($input, $value);*/
211+ $ value = require_field ($ input , $ value , '<kbd> ' .$ input ['label ' ] . '</kbd> ' . 'Bu alan boş bırakılamaz. ' );
212+ return $ value ;
213+ }
214+
215+ function require_field ($ input , $ field_value , $ error = null ){
216+ if ($ input ['require ' ] == true ){
217+ if (empty ($ field_value )){
218+ add_settings_error ('RedirectPageOptions_section ' , 'require_error ' , $ error );
219+ $ field_value = get_option ( $ input ['id ' ] );
220+ }
221+ }
222+ return $ field_value ;
223+ }
224+ }
225+
226+ public function field_cb ( $ field ) {
227+ default_value ($ field );
228+ $ value = get_option ( $ field ['id ' ] );
229+ $ placeholder = '' ;
230+ if ( isset ($ field ['placeholder ' ]) ) {
231+ $ placeholder = $ field ['placeholder ' ];
232+ }
233+ switch ( $ field ['type ' ] ) {
234+ case 'select ' :
235+ case 'multiselect ' :
236+ if ( ! empty ( $ field ['options ' ] ) && is_array ( $ field ['options ' ] ) ) {
237+ $ attr = '' ;
238+ $ options = '' ;
239+ foreach ( $ field ['options ' ] as $ key => $ label ) {
240+ $ options .= sprintf ('<option value="%s" %s>%s</option> ' ,
241+ $ key ,
242+ selected ($ value , $ key , false ),
243+ $ label
244+ );
245+ }
246+ if ( $ field ['type ' ] === 'multiselect ' ){
247+ $ attr = ' multiple="multiple" ' ;
248+ }
249+ printf ( '<select name="%1$s" id="%1$s" %2$s>%3$s</select> ' ,
250+ $ field ['id ' ],
251+ $ attr ,
252+ $ options
253+ );
254+ }
255+ break ;
256+
257+ case 'textarea ' :
258+ printf ( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea> ' ,
259+ $ field ['id ' ],
260+ $ placeholder ,
261+ $ value
262+ );
263+ break ;
264+
265+ default :
266+ printf ( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" /> ' ,
267+ $ field ['id ' ],
268+ $ field ['type ' ],
269+ $ placeholder ,
270+ $ value
271+ );
272+ }
273+ if ( isset ($ field ['desc ' ]) ) {
274+ if ( $ desc = $ field ['desc ' ] ) {
275+ ?> <div class="description"><?php
276+ echo $ desc ;
277+ ?> </div><?php
278+ }
279+ }
280+ }
281+
282+ }
283+ new RedirectPage_Settings_Page ();
284+
285+ function rp_time_shortcode (){
286+ $ time = get_option ('delay_time ' );
287+ return $ time ;
288+ }
289+ add_shortcode ( 'rp_delay_time ' , 'rp_time_shortcode ' );
290+
291+ function rp_pageID_shortcode (){
292+ $ p_id = get_option ('custom_page_id ' );
293+ return $ p_id ;
294+ }
295+ add_shortcode ( 'rp_page_ID ' , 'rp_pageID_shortcode ' );
296+
297+ function rp_title_shortcode (){
298+ $ title = get_option ('redirect_page_title ' );
299+ return $ title ;
300+ }
301+ add_shortcode ( 'rp_title ' , 'rp_title_shortcode ' );
0 commit comments