| @@ -1,177 +1,341 @@ | ||
| 1 | -<?php | |
| 2 | -namespace LearnPress\TemplateHooks\Admin; | |
| 3 | - | |
| 4 | -use LearnPress\Helpers\Template; | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * Template Show list items to select in popup. | |
| 8 | - * | |
| 9 | - * @since 4.2.9 | |
| 10 | - * @version 1.0.1 | |
| 11 | - */ | |
| 12 | -class AdminTemplate { | |
| 13 | - /** | |
| 14 | - * HTML TinyMCE editor | |
| 15 | - * | |
| 16 | - * @param string $value | |
| 17 | - * @param string $id_name | |
| 18 | - * @param array $setting | |
| 19 | - * | |
| 20 | - * @return string | |
| 21 | - * @since 4.2.9 | |
| 22 | - * @version 1.0.0 | |
| 23 | - */ | |
| 24 | - public static function editor_tinymce( string $value, string $id_name, array $setting = [] ): string { | |
| 25 | - $args = array_merge( | |
| 26 | - [ | |
| 27 | - 'default_editor' => 'tinymce', | |
| 28 | - 'media_buttons' => true, | |
| 29 | - 'editor_class' => 'lp-editor-tinymce', | |
| 30 | - 'editor_height' => 210, | |
| 31 | - ], | |
| 32 | - $setting | |
| 33 | - ); | |
| 34 | - | |
| 35 | - ob_start(); | |
| 36 | - wp_editor( | |
| 37 | - $value, | |
| 38 | - $id_name, | |
| 39 | - $args | |
| 40 | - ); | |
| 41 | - | |
| 42 | - return ob_get_clean(); | |
| 43 | - } | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * HTML for popup items to select. | |
| 47 | - * | |
| 48 | - * @param array $tabs [ [ key => label ] ]. | |
| 49 | - * @param string $html_items | |
| 50 | - * | |
| 51 | - * @return string | |
| 52 | - */ | |
| 53 | - public static function html_popup_items_to_select_clone( array $tabs, string $html_items ): string { | |
| 54 | - $html_tabs = ''; | |
| 55 | - $i = 0; | |
| 56 | - foreach ( $tabs as $key => $label ) { | |
| 57 | - $tab_active = ''; | |
| 58 | - if ( $i === 0 ) { | |
| 59 | - $tab_active = 'active'; | |
| 60 | - ++$i; | |
| 61 | - } | |
| 62 | - | |
| 63 | - $html_tabs .= sprintf( | |
| 64 | - '<li data-type="%s" class="tab %s"><a href="#">%s</a></li>', | |
| 65 | - $key, | |
| 66 | - $tab_active, | |
| 67 | - $label | |
| 68 | - ); | |
| 69 | - } | |
| 70 | - | |
| 71 | - $section_header = [ | |
| 72 | - 'wrap' => '<div class="header">', | |
| 73 | - 'count' => '<div class="header-count-items-selected lp-hidden"></div>', | |
| 74 | - 'tabs' => sprintf( | |
| 75 | - '<ul class="tabs">%s</ul>', | |
| 76 | - $html_tabs | |
| 77 | - ), | |
| 78 | - 'wrap_end' => '</div>', | |
| 79 | - ]; | |
| 80 | - | |
| 81 | - $section_main = [ | |
| 82 | - 'wrap' => '<div class="main">', | |
| 83 | - 'wrap_items' => '<div class="list-items-wrap">', | |
| 84 | - 'search' => sprintf( | |
| 85 | - '<input class="%1$s" name="%1$s" type="text" placeholder="%2$s">', | |
| 86 | - 'lp-search-title-item', | |
| 87 | - __( 'Type here to search for an item', 'learnpress' ) | |
| 88 | - ), | |
| 89 | - 'list-items' => $html_items, | |
| 90 | - 'wrap_items_end' => '</div>', | |
| 91 | - 'list-items-selected' => ' | |
| 92 | - <ul class="list-items-selected lp-hidden"> | |
| 93 | - <li class="li-item-selected clone lp-hidden" data-id="" data-type=""> | |
| 94 | - <i class="dashicons dashicons-remove"></i> | |
| 95 | - <div class="title-display"></div> | |
| 96 | - </li> | |
| 97 | - </ul>', | |
| 98 | - 'wrap_end' => '</div>', | |
| 99 | - ]; | |
| 100 | - | |
| 101 | - $section_footer = [ | |
| 102 | - 'wrap' => '<div class="footer">', | |
| 103 | - 'btn-add' => sprintf( | |
| 104 | - '<button type="button" disabled="disabled" class="button lp-btn-add-items-selected lp-btn-edit-primary">%s</button>', | |
| 105 | - __( 'Add', 'learnpress' ) | |
| 106 | - ), | |
| 107 | - 'count-items-selected' => sprintf( | |
| 108 | - '<button type="button" disabled="disabled" class="button lp-btn-count-items-selected">%s %s</button>', | |
| 109 | - sprintf( __( 'Selected items', 'learnpress' ), 0 ), | |
| 110 | - '<span class="count"></span>' | |
| 111 | - ), | |
| 112 | - 'btn-back' => sprintf( | |
| 113 | - '<button type="button" class="button lp-btn-back-to-select-items lp-hidden">%s</button>', | |
| 114 | - __( 'Back', 'learnpress' ) | |
| 115 | - ), | |
| 116 | - 'wrap_end' => '</div>', | |
| 117 | - ]; | |
| 118 | - | |
| 119 | - $section = [ | |
| 120 | - 'wrap' => '<div class="lp-popup-items-to-select">', | |
| 121 | - 'header' => Template::combine_components( $section_header ), | |
| 122 | - 'main' => Template::combine_components( $section_main ), | |
| 123 | - 'footer' => Template::combine_components( $section_footer ), | |
| 124 | - 'wrap_end' => '</div>', | |
| 125 | - ]; | |
| 126 | - | |
| 127 | - return Template::combine_components( $section ); | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * HTML for tom select. | |
| 132 | - * | |
| 133 | - * @param array $args { | |
| 134 | - * Arguments. | |
| 135 | - * | |
| 136 | - * @type array $options Options for select. | |
| 137 | - * @type string $name Name attribute for select. | |
| 138 | - * @type string $class_name Class name for select. | |
| 139 | - * } | |
| 140 | - * | |
| 141 | - * @return string | |
| 142 | - * @since 4.3.0 | |
| 143 | - * @version 1.0.0 | |
| 144 | - */ | |
| 145 | - public static function html_tom_select( array $args = [] ): string { | |
| 146 | - $html_options = ''; | |
| 147 | - | |
| 148 | - $options = $args['options'] ?? []; | |
| 149 | - $name = $args['name'] ?? ''; | |
| 150 | - $class_name = $args['class_name'] ?? ''; | |
| 151 | - $default_value = $args['default_value'] ?? ''; | |
| 152 | - $multiple = $args['multiple'] ?? false; | |
| 153 | - $multiple = $multiple ? 'multiple' : ''; | |
| 154 | - foreach ( $options as $key => $value ) { | |
| 155 | - if ( is_array( $default_value ) ) { | |
| 156 | - $selected = in_array( $key, $default_value, true ) ? 'selected' : ''; | |
| 157 | - } else { | |
| 158 | - $selected = selected( $default_value, $key, false ); | |
| 159 | - } | |
| 160 | - | |
| 161 | - $html_options .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), $selected, esc_html( $value ) ); | |
| 162 | - } | |
| 163 | - | |
| 164 | - $section = [ | |
| 165 | - 'select' => sprintf( | |
| 166 | - '<select name="%s" class="%s lp-tom-select" %s>', | |
| 167 | - esc_attr( $name ), | |
| 168 | - esc_attr( $class_name ), | |
| 169 | - $multiple | |
| 170 | - ), | |
| 171 | - 'options' => $html_options, | |
| 172 | - 'select-end' => '</select>', | |
| 173 | - ]; | |
| 174 | - | |
| 175 | - return Template::combine_components( $section ); | |
| 176 | - } | |
| 177 | -} | |
| 1 | +<?php | |
| 2 | +namespace LearnPress\TemplateHooks\Admin; | |
| 3 | + | |
| 4 | +use LearnPress\Helpers\Template; | |
| 5 | +use LP_Request; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Template Show list items to select in popup. | |
| 9 | + * | |
| 10 | + * @since 4.2.9 | |
| 11 | + * @version 1.0.1 | |
| 12 | + */ | |
| 13 | +class AdminTemplate { | |
| 14 | + /** | |
| 15 | + * HTML TinyMCE editor | |
| 16 | + * | |
| 17 | + * @param string $value | |
| 18 | + * @param string $id_name | |
| 19 | + * @param array $setting | |
| 20 | + * | |
| 21 | + * @return string | |
| 22 | + * @since 4.2.9 | |
| 23 | + * @version 1.0.0 | |
| 24 | + */ | |
| 25 | + public static function editor_tinymce( string $value, string $id_name, array $setting = [] ): string { | |
| 26 | + $args = array_merge( | |
| 27 | + [ | |
| 28 | + 'default_editor' => 'tinymce', | |
| 29 | + 'media_buttons' => true, | |
| 30 | + 'editor_class' => 'lp-editor-tinymce', | |
| 31 | + 'editor_height' => 210, | |
| 32 | + ], | |
| 33 | + $setting | |
| 34 | + ); | |
| 35 | + | |
| 36 | + ob_start(); | |
| 37 | + wp_editor( | |
| 38 | + $value, | |
| 39 | + $id_name, | |
| 40 | + $args | |
| 41 | + ); | |
| 42 | + | |
| 43 | + return ob_get_clean(); | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * HTML for popup items to select. | |
| 48 | + * | |
| 49 | + * @param array $tabs [ [ key => label ] ]. | |
| 50 | + * @param string $html_items | |
| 51 | + * @param string $html_filter_fields @since LP v4.4.6 | |
| 52 | + * @param array $extra_data @since LP v4.4.6 { 'classes' => '' } | |
| 53 | + * @return string | |
| 54 | + * @since 4.2.8.7.1 | |
| 55 | + * @version 1.0.1 | |
| 56 | + */ | |
| 57 | + public static function html_popup_items_to_select_clone( | |
| 58 | + array $tabs, | |
| 59 | + string $html_items, | |
| 60 | + string $html_filter_fields = '', | |
| 61 | + array $extra_data = [] | |
| 62 | + ): string { | |
| 63 | + $html_tabs = ''; | |
| 64 | + $i = 0; | |
| 65 | + foreach ( $tabs as $key => $label ) { | |
| 66 | + $tab_active = ''; | |
| 67 | + if ( $i === 0 ) { | |
| 68 | + $tab_active = 'active'; | |
| 69 | + ++$i; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $html_tabs .= sprintf( | |
| 73 | + '<li data-type="%s" class="tab %s"><a href="#">%s</a></li>', | |
| 74 | + $key, | |
| 75 | + $tab_active, | |
| 76 | + $label | |
| 77 | + ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + $section_header = [ | |
| 81 | + 'wrap' => '<div class="header">', | |
| 82 | + 'count' => '<div class="header-count-items-selected lp-hidden"></div>', | |
| 83 | + 'tabs' => sprintf( | |
| 84 | + '<ul class="tabs">%s</ul>', | |
| 85 | + $html_tabs | |
| 86 | + ), | |
| 87 | + 'wrap_end' => '</div>', | |
| 88 | + ]; | |
| 89 | + | |
| 90 | + $section_main = [ | |
| 91 | + 'wrap' => '<div class="main">', | |
| 92 | + 'wrap_items' => '<div class="list-items-wrap">', | |
| 93 | + 'search' => ! empty( $html_filter_fields ) ? $html_filter_fields : sprintf( | |
| 94 | + '<input class="%1$s" name="%1$s" type="text" placeholder="%2$s">', | |
| 95 | + 'lp-search-title-item', | |
| 96 | + __( 'Type here to search for an item', 'learnpress' ) | |
| 97 | + ), | |
| 98 | + 'list-items' => $html_items, | |
| 99 | + 'wrap_items_end' => '</div>', | |
| 100 | + 'list-items-selected' => ' | |
| 101 | + <ul class="list-items-selected lp-hidden"> | |
| 102 | + <li class="li-item-selected clone lp-hidden" data-id="" data-type=""> | |
| 103 | + <i class="dashicons dashicons-remove"></i> | |
| 104 | + <div class="title-display"></div> | |
| 105 | + </li> | |
| 106 | + </ul>', | |
| 107 | + 'wrap_end' => '</div>', | |
| 108 | + ]; | |
| 109 | + | |
| 110 | + $section_footer = [ | |
| 111 | + 'wrap' => '<div class="footer">', | |
| 112 | + 'btn-add' => sprintf( | |
| 113 | + '<button type="button" disabled="disabled" | |
| 114 | + class="button lp-btn-add-items-selected lp-btn-edit-primary %s" %s>%s</button>', | |
| 115 | + esc_attr( $extra_data['btn-add-classes'] ?? '' ), | |
| 116 | + $extra_data['btn-add-attrs'] ?? '', // No esc_attr here | |
| 117 | + $extra_data['btn-add-label'] ?? __( 'Add', 'learnpress' ) | |
| 118 | + ), | |
| 119 | + 'count-items-selected' => sprintf( | |
| 120 | + '<button type="button" disabled="disabled" class="button lp-btn-count-items-selected">%s %s</button>', | |
| 121 | + sprintf( __( 'Selected items', 'learnpress' ), 0 ), | |
| 122 | + '<span class="count"></span>' | |
| 123 | + ), | |
| 124 | + 'btn-back' => sprintf( | |
| 125 | + '<button type="button" class="button lp-btn-back-to-select-items lp-hidden">%s</button>', | |
| 126 | + __( 'Back', 'learnpress' ) | |
| 127 | + ), | |
| 128 | + 'btn-custom' => $extra_data['btn-custom'] ?? '', | |
| 129 | + 'wrap_end' => '</div>', | |
| 130 | + ]; | |
| 131 | + | |
| 132 | + $section = [ | |
| 133 | + 'wrap' => sprintf( | |
| 134 | + '<div class="lp-popup-items-to-select %s">', | |
| 135 | + esc_attr( $extra_data['classes'] ?? '' ) | |
| 136 | + ), | |
| 137 | + 'header' => Template::combine_components( $section_header ), | |
| 138 | + 'main' => Template::combine_components( $section_main ), | |
| 139 | + 'footer' => Template::combine_components( $section_footer ), | |
| 140 | + 'wrap_end' => '</div>', | |
| 141 | + ]; | |
| 142 | + | |
| 143 | + return Template::combine_components( $section ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * HTML for tom select. | |
| 148 | + * | |
| 149 | + * @param array $args { | |
| 150 | + * Arguments. | |
| 151 | + * | |
| 152 | + * @type array $options Options for select. | |
| 153 | + * @type string $name Name attribute for select. | |
| 154 | + * @type string $class_name Class name for select. | |
| 155 | + * } | |
| 156 | + * | |
| 157 | + * @return string | |
| 158 | + * @since 4.3.0 | |
| 159 | + * @version 1.0.1 | |
| 160 | + */ | |
| 161 | + public static function html_tom_select( array $args = [] ): string { | |
| 162 | + $html_options = ''; | |
| 163 | + | |
| 164 | + $options = $args['options'] ?? []; | |
| 165 | + $name = $args['name'] ?? ''; | |
| 166 | + $class_name = $args['class_name'] ?? ''; | |
| 167 | + $default_value = $args['default_value'] ?? ''; | |
| 168 | + $multiple = $args['multiple'] ?? false; | |
| 169 | + $multiple = $multiple ? 'multiple' : ''; | |
| 170 | + $struct = $args['struct'] ?? []; | |
| 171 | + $saved = $args['saved'] ?? ''; | |
| 172 | + foreach ( $options as $key => $value ) { | |
| 173 | + if ( is_array( $default_value ) ) { | |
| 174 | + $selected = in_array( $key, $default_value, true ) ? 'selected' : ''; | |
| 175 | + } else { | |
| 176 | + $selected = selected( $default_value, $key, false ); | |
| 177 | + } | |
| 178 | + | |
| 179 | + $html_options .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), $selected, esc_html( $value ) ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + $section = [ | |
| 183 | + 'select' => sprintf( | |
| 184 | + '<select name="%s" class="%s lp-tom-select" %s data-struct="%s" data-save="%s">', | |
| 185 | + esc_attr( $name ), | |
| 186 | + esc_attr( $class_name ), | |
| 187 | + esc_attr( $multiple ), | |
| 188 | + esc_attr( htmlentities2( json_encode( $struct ) ) ), | |
| 189 | + esc_attr( htmlentities2( json_encode( $saved ) ) ), | |
| 190 | + ), | |
| 191 | + 'options' => $html_options, | |
| 192 | + 'select-end' => '</select>', | |
| 193 | + ]; | |
| 194 | + | |
| 195 | + return Template::combine_components( $section ); | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 199 | + * Render a toggle switch (checkbox + track) for enabling or disabling a setting. | |
| 200 | + * | |
| 201 | + * @param array $data Toggle configuration. Accepts: | |
| 202 | + * - classes: CSS classes for the input. | |
| 203 | + * - name: Input name attribute (default: 'lp_toggle_enable'). | |
| 204 | + * - value: Current value (truthy/falsy). | |
| 205 | + * | |
| 206 | + * @return string HTML markup for the toggle switch. | |
| 207 | + * @since 4.4.5 | |
| 208 | + * @version 1.0.0 | |
| 209 | + */ | |
| 210 | + public static function html_toggle_enable( array $data = [] ): string { | |
| 211 | + $classes = $data['classes'] ?? ''; | |
| 212 | + $name = $data['name'] ?? 'lp_toggle_enable'; | |
| 213 | + $value = $data['value'] ?? 0; | |
| 214 | + $checked = $value ? 'checked' : ''; | |
| 215 | + | |
| 216 | + $section = [ | |
| 217 | + 'wrapper' => sprintf( | |
| 218 | + '<label class="lp-toggle-enable %s">', | |
| 219 | + $value ? 'is-enabled' : '' | |
| 220 | + ), | |
| 221 | + 'input' => sprintf( | |
| 222 | + '<input type="checkbox" class="lp-toggle-enable__input %s" | |
| 223 | + name="%s" | |
| 224 | + value="%s" | |
| 225 | + data-info="%s" | |
| 226 | + %s/>', | |
| 227 | + esc_attr( $classes ), | |
| 228 | + esc_attr( $name ), | |
| 229 | + esc_attr( $value ), | |
| 230 | + esc_attr( Template::convert_data_to_json( $data ) ), | |
| 231 | + esc_attr( $checked ) | |
| 232 | + ), | |
| 233 | + 'track' => '<span class="lp-toggle-enable__track"></span>', | |
| 234 | + 'wrapper-end' => '</label>', | |
| 235 | + ]; | |
| 236 | + | |
| 237 | + return Template::combine_components( $section ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * HTML form filter | |
| 242 | + * | |
| 243 | + * @param array $data | |
| 244 | + * @return string | |
| 245 | + * @since 4.4.5 | |
| 246 | + * @version 1.0.0 | |
| 247 | + */ | |
| 248 | + public static function html_form_filter( array $data = [] ): string { | |
| 249 | + $form_classes = $data['form_classes'] ?? ''; | |
| 250 | + $id = $data['id'] ?? ''; | |
| 251 | + $html_fields = $data['fields'] ?? ''; | |
| 252 | + $html_btn_actions = $data['btn_actions'] ?? ''; | |
| 253 | + | |
| 254 | + $sections = array( | |
| 255 | + 'wrap' => sprintf( | |
| 256 | + '<form class="lp-form-filter %s"%s>', | |
| 257 | + esc_attr( $form_classes ), | |
| 258 | + $id ? sprintf( ' id="%s"', esc_attr( $id ) ) : '' | |
| 259 | + ), | |
| 260 | + 'fields' => sprintf( | |
| 261 | + '<div class="lp-form-filter__fields">%s</div>', | |
| 262 | + $html_fields | |
| 263 | + ), | |
| 264 | + 'btn-actions' => sprintf( | |
| 265 | + '<div class="lp-form-filter__actions">%s</div>', | |
| 266 | + $html_btn_actions | |
| 267 | + ), | |
| 268 | + 'wrap_end' => '</form>', | |
| 269 | + ); | |
| 270 | + | |
| 271 | + return Template::combine_components( $sections ); | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * Display content html by format WP Admin Screen | |
| 276 | + * | |
| 277 | + * @param array $data ['tabs' => [ 'tab' => 'name' ], 'content' => '', 'title' => '', 'id' => '']. | |
| 278 | + * @return string | |
| 279 | + * @since 4.4.5 | |
| 280 | + * @version 1.0.0 | |
| 281 | + */ | |
| 282 | + public static function html_on_wp_admin_screen( array $data = [] ): string { | |
| 283 | + $tabs = $data['tabs'] ?? []; | |
| 284 | + $active_tab = LP_Request::get_param( 'tab' ); | |
| 285 | + if ( empty( $active_tab ) || ! isset( $tabs[ $active_tab ] ) ) { | |
| 286 | + $tab_keys = array_keys( $tabs ); | |
| 287 | + $active_tab = reset( $tab_keys ); | |
| 288 | + } | |
| 289 | + $content = $data['content'] ?? ''; | |
| 290 | + $title = $data['title'] ?? ''; | |
| 291 | + $id = $data['id'] ?? ''; | |
| 292 | + | |
| 293 | + $classes = array( 'wrap' ); | |
| 294 | + if ( $id ) { | |
| 295 | + $classes[] = $id; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $html_tabs = ''; | |
| 299 | + if ( $tabs ) { | |
| 300 | + foreach ( $tabs as $tab => $tab_title ) { | |
| 301 | + $active_class = ( $tab == $active_tab ) ? ' nav-tab-active' : ''; | |
| 302 | + | |
| 303 | + if ( $active_class ) { | |
| 304 | + $html_tabs .= sprintf( | |
| 305 | + '<span class="nav-tab%s">%s</span>', | |
| 306 | + esc_attr( $active_class ), | |
| 307 | + esc_html( $tab_title ) | |
| 308 | + ); | |
| 309 | + } else { | |
| 310 | + $html_tabs .= sprintf( | |
| 311 | + '<a class="nav-tab" href="?page=%s&tab=%s">%s</a>', | |
| 312 | + esc_attr( $id ), | |
| 313 | + esc_attr( $tab ), | |
| 314 | + esc_html( $tab_title ) | |
| 315 | + ); | |
| 316 | + } | |
| 317 | + } | |
| 318 | + } | |
| 319 | + | |
| 320 | + $sections = array( | |
| 321 | + 'wrap' => sprintf( | |
| 322 | + '<div class="%s">', | |
| 323 | + esc_attr( implode( ' ', $classes ) ) | |
| 324 | + ), | |
| 325 | + 'heading' => $title ? sprintf( | |
| 326 | + '<h1 class="wp-heading-inline">%s</h1>', | |
| 327 | + wp_kses_post( $title ) | |
| 328 | + ) : '', | |
| 329 | + 'tabs' => $tabs ? sprintf( | |
| 330 | + '<h2 class="nav-tab-wrapper">%s</h2>', | |
| 331 | + $html_tabs | |
| 332 | + ) : '', | |
| 333 | + 'wrap-content' => '<div class="lp-admin-tabs">', | |
| 334 | + 'content' => $content, | |
| 335 | + 'wrap-content-end' => '</div>', | |
| 336 | + 'wrap-end' => '</div>', | |
| 337 | + ); | |
| 338 | + | |
| 339 | + return Template::combine_components( $sections ); | |
| 340 | + } | |
| 341 | +} | |