PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / abstract-settings.php

abstract-settings.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.7, at inc/abstract-settings.php

205 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Abstract_Settings
4 */
5
6 use LearnPress\TemplateHooks\Admin\AdminSettingsPage;
7
8 abstract class LP_Abstract_Settings {
9
10 /**
11 * LP_Abstract_Settings constructor.
12 */
13 public function __construct() {
14 add_filter( 'learn-press/update-settings/redirect', array( $this, '_do_save' ) );
15 }
16
17 public function _do_save( $url ) {
18 $this->save();
19
20 return $url;
21 }
22
23 public function save() {
24 // This function should be overwritten from its child
25 }
26
27 /**
28 * @return bool|mixed|array
29 */
30 public function get_settings() {
31 return false;
32 }
33
34 /**
35 * Get name for field
36 *
37 * @param $name
38 *
39 * @return mixed
40 */
41 public function get_admin_field_name( $name ) {
42 $tab = AdminSettingsPage::instance()->get_active_tab();
43 $section = AdminSettingsPage::instance()->get_active_section();
44
45 if ( $tab === 'payments' && $section !== 'general' && ! empty( $name ) ) {
46 if ( strpos( $name, '[' ) === 0 ) {
47 $name = $section . $name;
48 } else {
49 $name = $section . '_' . $name;
50 }
51 }
52
53 if ( empty( $name ) ) {
54 $name = md5( microtime( true ) );
55 }
56
57 $field_name = apply_filters( 'learn_press_settings_field_name_' . $name, "learn_press_{$name}" );
58
59 return $field_name;
60 }
61
62 /**
63 * Get ID for field
64 *
65 * @param $name
66 *
67 * @return mixed
68 */
69 public function get_admin_field_id( $name ) {
70 return preg_replace( array( '!\[|(\]\[)!', '!\]!' ), array( '_', '' ), $this->get_field_name( $name ) );
71 }
72
73 /**
74 * Print admin fields options.
75 *
76 * @version 4.0.0
77 */
78 public function admin_option_settings() {
79 $settings = $this->get_settings();
80 $settings = $this->sanitize_settings( $settings );
81
82 do_action( 'learn-press/settings-render' );
83
84 if ( $settings ) {
85 LP_Meta_Box_Helper::output_fields( $settings );
86 } else {
87 echo esc_html__( 'No settings available.', 'learnpress' );
88 }
89 }
90
91 /**
92 * Sanitize settings before rendering.
93 * Fill std from database, reformat conditional fields...
94 *
95 * @param $settings
96 *
97 * @return mixed
98 */
99 public function sanitize_settings( $settings ) {
100 if ( $settings ) {
101 foreach ( $settings as $k => $field ) {
102
103 // except heading options.
104 if ( isset( $field['id'] ) ) {
105
106 $field['id'] = $this->get_admin_field_name( $field['id'] );
107
108 // A field is an array of values, find the real name.
109 if ( strpos( $field['id'], '[' ) !== false ) {
110 parse_str( $field['id'], $group );
111 $keys = array_keys( $group );
112 $option_name = reset( $keys );
113 } else {
114 $option_name = $field['id'];
115 }
116
117 // Get value from option
118 $std = get_option( $option_name );
119 if ( false === $std ) {
120 $std = array_key_exists( 'default', $field ) ? $field['default'] : '';
121 }
122
123 // If the field is an array
124 if ( isset( $group ) && is_array( $std ) ) {
125 $loop = 0;
126 while ( is_array( $group ) && $loop ++ < 10 ) {
127 if ( ! empty( $group[ $option_name ] ) ) {
128 $option_keys = array_keys( $group[ $option_name ] );
129 $option_name = reset( $option_keys );
130 $group = ! empty( $group[ $option_name ] ) ? $group[ $option_name ] : false;
131 $std = ! empty( $std[ $option_name ] ) ? $std[ $option_name ] : false;
132 }
133 }
134 }
135 $field['std'] = apply_filters( 'learn-press/settings/default-field-value', $std, $field );
136 $field['learn-press-settings'] = 'yes';
137 $this->parse_conditional( $field );
138 $settings[ $k ] = $field;
139 }
140 }
141 }
142
143 return $settings;
144 }
145
146 public function parse_conditional( &$field ) {
147 // Re-format conditional logic fields
148 if ( ! empty( $field['visibility'] ) ) {
149 $conditional = $field['visibility'];
150
151 if ( ! array_key_exists( 0, $conditional['conditional'] ) ) {
152 $conditional['conditional'] = array(
153 $conditional['conditional'],
154 );
155 }
156
157 foreach ( $conditional['conditional'] as $kk => $conditional_field ) {
158 $conditional['conditional'][ $kk ]['field'] = $this->get_admin_field_name( $conditional_field['field'] );
159 }
160
161 $field['visibility'] = $conditional;
162 }
163
164 return $field;
165 }
166
167 /**
168 * @param $option_name
169 * @param null $default
170 *
171 * @return array|null|string
172 */
173 public function get_option( $option_name, $default = null ) {
174 if ( strstr( $option_name, '[' ) ) {
175 parse_str( $option_name, $option_array );
176
177 // Option name is first key
178 $option_name = current( array_keys( $option_array ) );
179
180 // Get value
181 $option_values = get_option( $option_name, '' );
182
183 $key = key( $option_array[ $option_name ] );
184
185 if ( isset( $option_values[ $key ] ) ) {
186 $option_value = $option_values[ $key ];
187 } else {
188 $option_value = null;
189 }
190
191 // Single value
192 } else {
193 $option_value = LP_Settings::instance()->get( preg_replace( '!^learn_press_!', '', $option_name ), null );
194 }
195
196 if ( is_array( $option_value ) ) {
197 $option_value = array_map( 'stripslashes', $option_value );
198 } elseif ( ! is_null( $option_value ) ) {
199 $option_value = stripslashes( $option_value );
200 }
201
202 return $option_value === null ? $default : $option_value;
203 }
204 }
205