Form
1 week ago
Migrations
1 week ago
Option
1 week ago
Route
1 week ago
Tab
1 week ago
Forms.php
1 week ago
Menu.php
1 week ago
MigrationsManager.php
1 week ago
Page.php
1 week ago
Routes.php
1 week ago
Tabs.php
1 week ago
Page.php
288 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Settings; |
| 4 | |
| 5 | use FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 6 | use FcfVendor\WPDesk\PluginBuilder\Plugin\HookablePluginDependant; |
| 7 | use FcfVendor\WPDesk\PluginBuilder\Plugin\PluginAccess; |
| 8 | use FcfVendor\WPDesk\View\Renderer\SimplePhpRenderer; |
| 9 | use FcfVendor\WPDesk\View\Resolver\DirResolver; |
| 10 | use WPDesk\FCF\Free\Field\Types; |
| 11 | use WPDesk\FCF\Free\Settings\Route\RouteIntegration; |
| 12 | |
| 13 | /** |
| 14 | * Supports page of plugin settings. |
| 15 | */ |
| 16 | class Page implements Hookable, HookablePluginDependant { |
| 17 | |
| 18 | use PluginAccess; |
| 19 | |
| 20 | public const SETTINGS_PAGE = 'wpdesk_checkout_fields_settings'; |
| 21 | |
| 22 | private const SCRIPT_HANDLE = 'fcf-admin'; |
| 23 | |
| 24 | /** |
| 25 | * Class object for template rendering. |
| 26 | * |
| 27 | * @var SimplePhpRenderer |
| 28 | */ |
| 29 | private $renderer; |
| 30 | |
| 31 | /** |
| 32 | * Class constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->set_renderer(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Init class for template rendering. |
| 40 | */ |
| 41 | private function set_renderer() { |
| 42 | $this->renderer = new SimplePhpRenderer( new DirResolver( dirname( dirname( __DIR__ ) ) . '/templates' ) ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public function hooks() { |
| 49 | add_action( 'admin_menu', [ $this, 'add_settings_page' ], 80 ); |
| 50 | add_action( 'admin_enqueue_scripts', [ $this, 'load_assets_for_settings_page' ], 80 ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Registers admin page for plugin settings. |
| 55 | * |
| 56 | * @internal |
| 57 | */ |
| 58 | public function add_settings_page() { |
| 59 | add_submenu_page( |
| 60 | 'woocommerce', |
| 61 | __( 'Checkout Fields Settings', 'flexible-checkout-fields' ), |
| 62 | __( 'Checkout Fields', 'flexible-checkout-fields' ), |
| 63 | 'manage_woocommerce', |
| 64 | self::SETTINGS_PAGE, |
| 65 | [ $this, 'render_settings_page' ] |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Initiates loading of assets needed to operate admin page. |
| 71 | * |
| 72 | * @internal |
| 73 | */ |
| 74 | public function load_assets_for_settings_page() { |
| 75 | if ( ! isset( $_GET['page'] ) || ( $_GET['page'] !== self::SETTINGS_PAGE ) ) { // phpcs:ignore |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | add_filter( 'admin_footer_text', [ $this, 'update_footer_text' ] ); |
| 80 | $this->load_styles_for_page(); |
| 81 | $this->load_scripts_for_page(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Loads admin page template. |
| 86 | * |
| 87 | * @internal |
| 88 | */ |
| 89 | public function render_settings_page() { |
| 90 | $menu_tabs = ( new Menu() )->get_menu_tabs(); |
| 91 | $menu_sections = ( new Menu() )->get_menu_sections(); |
| 92 | |
| 93 | echo $this->renderer->render( // phpcs:ignore |
| 94 | 'views/admin-page', |
| 95 | [ |
| 96 | 'settings' => $this->load_settings_data( $menu_sections ), // phpcs:ignore |
| 97 | 'menu_tabs' => $menu_tabs, // phpcs:ignore |
| 98 | 'menu_sections' => $menu_sections, // phpcs:ignore |
| 99 | ] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns set of data needed to support admin panel by JS code. |
| 105 | * |
| 106 | * @param array $menu_sections Items of menu with sections. |
| 107 | * |
| 108 | * @return array Settings of admin page. |
| 109 | */ |
| 110 | private function load_settings_data( array $menu_sections ): array { |
| 111 | $settings = [ |
| 112 | 'rest_api_url' => get_rest_url( null, RouteIntegration::REST_API_NAMESPACE ), |
| 113 | 'header_nonce' => wp_create_nonce( 'wp_rest' ), |
| 114 | 'i18n' => $this->load_translations(), |
| 115 | 'is_pro_compatible' => apply_filters( 'flexible_checkout_fields/is_pro_compatible', true ), |
| 116 | ]; |
| 117 | |
| 118 | switch ( $_GET['tab'] ?? '' ) { // phpcs:ignore |
| 119 | case MENU::MENU_TAB_SETTINGS: |
| 120 | $settings['form_settings'] = [ |
| 121 | 'api_route' => 'settings', |
| 122 | 'form_index' => null, |
| 123 | 'option_fields' => apply_filters( 'flexible_checkout_fields/form_fields_settings', [] ), |
| 124 | 'option_values' => apply_filters( 'flexible_checkout_fields/form_data_settings', [] ), |
| 125 | 'settings_tabs' => [], |
| 126 | ]; |
| 127 | break; |
| 128 | default: |
| 129 | $section = $this->get_active_section( $menu_sections ); |
| 130 | if ( ! $section ) { |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | $settings['form_fields'] = [ |
| 135 | 'api_route' => sprintf( '%s/fields', $section['id'] ), |
| 136 | 'form_index' => $section['id'], |
| 137 | 'option_fields' => apply_filters( 'flexible_checkout_fields/field_types', [], $section['id'] ), |
| 138 | 'option_values' => array_values( apply_filters( 'flexible_checkout_fields/form_data_fields', [], $section['id'] ) ), |
| 139 | 'settings_tabs' => apply_filters( 'flexible_checkout_fields/field_settings_tabs', [] ), |
| 140 | 'field_group' => [ |
| 141 | [ |
| 142 | 'name' => Types::FIELD_GROUP_TEXT, |
| 143 | 'label' => __( 'Text Fields', 'flexible-checkout-fields' ), |
| 144 | ], |
| 145 | [ |
| 146 | 'name' => Types::FIELD_GROUP_OPTION, |
| 147 | 'label' => __( 'Option Fields', 'flexible-checkout-fields' ), |
| 148 | ], |
| 149 | [ |
| 150 | 'name' => Types::FIELD_GROUP_PICKER, |
| 151 | 'label' => __( 'Picker Fields', 'flexible-checkout-fields' ), |
| 152 | ], |
| 153 | [ |
| 154 | 'name' => Types::FIELD_GROUP_OTHER, |
| 155 | 'label' => __( 'Other Fields', 'flexible-checkout-fields' ), |
| 156 | ], |
| 157 | ], |
| 158 | ]; |
| 159 | |
| 160 | if ( ! $section['has_section_form'] ) { |
| 161 | break; |
| 162 | } |
| 163 | $settings['form_section'] = [ |
| 164 | 'api_route' => sprintf( '%s/section', $section['id'] ), |
| 165 | 'form_index' => $section['id'], |
| 166 | 'option_fields' => apply_filters( 'flexible_checkout_fields/form_fields_section', [], $section['id'] ), |
| 167 | 'option_values' => apply_filters( 'flexible_checkout_fields/form_data_section', [], $section['id'] ), |
| 168 | 'settings_tabs' => [], |
| 169 | ]; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Filter settings of admin page. |
| 175 | * |
| 176 | * @param array $settings Settings of admin page. |
| 177 | * @param array $menu_items Items of menu with sections. |
| 178 | */ |
| 179 | $settings = apply_filters( 'flexible_checkout_fields/init_admin_settings', $settings, $menu_sections ); |
| 180 | |
| 181 | return $settings; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns active section from sections menu. |
| 186 | * |
| 187 | * @param array $menu_sections Items of menu with sections. |
| 188 | * |
| 189 | * @return array|null Active item of menu. |
| 190 | */ |
| 191 | private function get_active_section( array $menu_sections ) { |
| 192 | foreach ( $menu_sections as $section ) { |
| 193 | if ( $section['is_active'] ) { |
| 194 | return $section; |
| 195 | } |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns list of translations used by JS code. |
| 202 | * |
| 203 | * @return array Translations values. |
| 204 | */ |
| 205 | private function load_translations(): array { |
| 206 | return [ |
| 207 | 'form_fields' => __( 'Edit form', 'flexible-checkout-fields' ), |
| 208 | 'form_add_field' => __( 'Add new field', 'flexible-checkout-fields' ), |
| 209 | 'form_section' => __( 'Edit section', 'flexible-checkout-fields' ), |
| 210 | 'form_settings' => __( 'Edit settings', 'flexible-checkout-fields' ), |
| 211 | 'button_add_field' => __( 'Add Field', 'flexible-checkout-fields' ), |
| 212 | 'button_add_row' => __( 'Add New', 'flexible-checkout-fields' ), |
| 213 | 'button_save' => __( 'Save Changes', 'flexible-checkout-fields' ), |
| 214 | 'button_reset' => __( 'Reset Section Settings', 'flexible-checkout-fields' ), |
| 215 | 'button_read_more' => __( 'Read more', 'flexible-checkout-fields' ), |
| 216 | 'button_yes' => __( 'Yes', 'flexible-checkout-fields' ), |
| 217 | 'button_no' => __( 'No', 'flexible-checkout-fields' ), |
| 218 | 'button_upload_image' => __( 'Upload image', 'flexible-checkout-fields' ), |
| 219 | 'button_select_color' => __( 'Select color', 'flexible-checkout-fields' ), |
| 220 | 'field_type' => __( 'Field Type', 'flexible-checkout-fields' ), |
| 221 | 'field_label' => __( 'Label', 'flexible-checkout-fields' ), |
| 222 | 'field_name' => __( 'Name', 'flexible-checkout-fields' ), |
| 223 | 'validation_required' => __( 'This field is required.', 'flexible-checkout-fields' ), |
| 224 | 'validation_max_length' => __( 'This value is too long.', 'flexible-checkout-fields' ), |
| 225 | 'validation_slug' => __( 'Field name should contains only lowercase letters, numbers and underscore sign.', 'flexible-checkout-fields' ), |
| 226 | 'select_placeholder' => __( 'Select...', 'flexible-checkout-fields' ), |
| 227 | 'select_loading' => __( 'Loading...', 'flexible-checkout-fields' ), |
| 228 | 'select_empty' => __( 'No options.', 'flexible-checkout-fields' ), |
| 229 | 'alert_field_unavailable' => sprintf( |
| 230 | /* translators: %1$s: break line, %2$s: anchor opening tag, %3$s: anchor closing tag */ |
| 231 | __( 'This field is available in the PRO version. %1$s %2$sUpgrade to PRO%3$s', 'flexible-checkout-fields' ), |
| 232 | '<br>', |
| 233 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-field-type-upgrade' ) ) . '" target="_blank" class="fcfArrowLink">', |
| 234 | '</a>' |
| 235 | ), |
| 236 | 'alert_remove_field' => __( 'Are you sure you want to delete this field? Deleting a field will remove it from all orders.', 'flexible-checkout-fields' ), |
| 237 | 'alert_reset' => __( 'Do you really want to reset section settings? Resetting a section remove all added fields from orders.', 'flexible-checkout-fields' ), |
| 238 | 'alert_no_fields' => __( 'No fields available.', 'flexible-checkout-fields' ), |
| 239 | 'alert_failed_save' => __( 'Failed to connect to WordPress REST API.', 'flexible-checkout-fields' ), |
| 240 | ]; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Removes WooCommerce footer from plugin settings page. |
| 245 | * |
| 246 | * @return string New footer content. |
| 247 | * @internal |
| 248 | */ |
| 249 | public function update_footer_text(): string { |
| 250 | return ''; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Enqueues styles in WordPress Admin Dashboard. |
| 255 | */ |
| 256 | private function load_styles_for_page() { |
| 257 | $is_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ); |
| 258 | |
| 259 | wp_register_style( |
| 260 | self::SCRIPT_HANDLE, |
| 261 | \trailingslashit( $this->plugin->get_plugin_assets_url() ) . 'css/new-admin.css', |
| 262 | [], |
| 263 | ( $is_debug ) ? time() : $this->plugin->get_script_version() |
| 264 | ); |
| 265 | wp_enqueue_style( self::SCRIPT_HANDLE ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Enqueues scripts in WordPress Admin Dashboard. |
| 270 | */ |
| 271 | private function load_scripts_for_page() { |
| 272 | $is_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ); |
| 273 | |
| 274 | wp_register_script( |
| 275 | self::SCRIPT_HANDLE, |
| 276 | \trailingslashit( $this->plugin->get_plugin_assets_url() ) . 'js/new-admin.js', |
| 277 | [ 'wp-i18n' ], |
| 278 | ( $is_debug ) ? time() : $this->plugin->get_script_version(), |
| 279 | true |
| 280 | ); |
| 281 | wp_enqueue_media(); |
| 282 | wp_enqueue_script( self::SCRIPT_HANDLE ); |
| 283 | |
| 284 | $plugin_dir = \trailingslashit( WP_PLUGIN_DIR ) . $this->plugin->get_plugin_file_path(); |
| 285 | wp_set_script_translations( self::SCRIPT_HANDLE, 'flexible-checkout-fields', \plugin_dir_path( $plugin_dir ) . 'lang' ); |
| 286 | } |
| 287 | } |
| 288 |