PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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.4, at inc/abstract-settings.php

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