Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.23% covered (warning)
69.23%
27 / 39
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Settings_Page
69.23% covered (warning)
69.23%
27 / 39
80.00% covered (warning)
80.00%
4 / 5
11.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 add_settings_page
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 display_plugin_admin_page
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 setup_sections
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 setup_fields
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The wp-admin settings page.
4 *
5 * @link       https://BrianHenry.ie
6 * @since      1.0.0
7 *
8 * @package    bh-wp-autologin-urls
9 */
10
11namespace BrianHenryIE\WP_Autologin_URLs\Admin;
12
13use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Enable_Magic_Links;
14use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Klaviyo_Private_Key;
15use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Log_Level;
16use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Use_WP_Login;
17use BrianHenryIE\WP_Autologin_URLs\Settings_Interface;
18use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Settings_Section_Element_Abstract;
19use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Admin_Enable;
20use BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields\Expiry_Age;
21use Psr\Log\LoggerAwareTrait;
22use Psr\Log\LoggerInterface;
23
24/**
25 * The setting page of the plugin.
26 */
27class Settings_Page {
28    use LoggerAwareTrait;
29
30    /**
31     * The settings, to pass to the individual fields for populating.
32     *
33     * @var Settings_Interface $settings The previously saved settings for the plugin.
34     */
35    protected Settings_Interface $settings;
36
37    /**
38     * Settings_Page constructor.
39     *
40     * @param Settings_Interface $settings The previously saved settings for the plugin.
41     * @param LoggerInterface    $logger A PSR logger.
42     */
43    public function __construct( Settings_Interface $settings, LoggerInterface $logger ) {
44        $this->settings = $settings;
45        $this->setLogger( $logger );
46    }
47
48    /**
49     * Add the Autologin URLs settings menu-item/page as a submenu-item of the Settings menu.
50     *
51     * /wp-admin/options-general.php?page=bh-wp-autologin-urls
52     *
53     * @hooked admin_menu
54     */
55    public function add_settings_page(): void {
56
57        add_options_page(
58            'Autologin URLs',
59            'Autologin URLs',
60            'manage_options',
61            $this->settings->get_plugin_slug(),
62            array( $this, 'display_plugin_admin_page' )
63        );
64    }
65
66    /**
67     * Registered above, called by WordPress to display the admin settings page.
68     */
69    public function display_plugin_admin_page(): void {
70
71        $logger      = $this->logger;
72        $example_url = site_url() . '/?autologin=' . get_current_user_id() . '~Yxu1UQG8IwJO';
73
74        $template = 'admin/settings-page.php';
75
76        $template_admin_settings_page = WP_PLUGIN_DIR . '/' . plugin_dir_path( $this->settings->get_plugin_basename() ) . 'templates/' . $template;
77
78        // Check the child theme for template overrides.
79        if ( file_exists( get_stylesheet_directory() . $template ) ) {
80            $template_admin_settings_page = get_stylesheet_directory() . $template;
81        } elseif ( file_exists( get_stylesheet_directory() . 'templates/' . $template ) ) {
82            $template_admin_settings_page = get_stylesheet_directory() . 'templates/' . $template;
83        }
84
85        /**
86         * Allow overriding the admin settings template.
87         */
88        $filtered_template_admin_settings_page = apply_filters( 'bh_wp_autologin_urls_admin_settings_page_template', $template_admin_settings_page );
89
90        if ( file_exists( $filtered_template_admin_settings_page ) ) {
91            include $filtered_template_admin_settings_page;
92        } else {
93            include $template_admin_settings_page;
94        }
95    }
96
97    /**
98     * Register the one settings section with WordPress.
99     *
100     * @hooked admin_init
101     */
102    public function setup_sections(): void {
103
104        $settings_page_slug_name = $this->settings->get_plugin_slug();
105
106        add_settings_section(
107            'default',
108            'Settings',
109            function () {},
110            $settings_page_slug_name
111        );
112    }
113
114    /**
115     * Field Configuration, each item in this array is one field/setting we want to capture.
116     *
117     * @hooked admin_init
118     *
119     * @see https://github.com/reside-eng/wordpress-custom-plugin/blob/master/admin/class-wordpress-custom-plugin-admin.php
120     *
121     * @since    1.0.0
122     */
123    public function setup_fields(): void {
124
125        $settings_page_slug_name = $this->settings->get_plugin_slug();
126
127        /**
128         * Other plugins (WooCommerce, WP Affiliate) handle this by using an array here, where each array element has
129         * a 'type' which is instantiated, then the defaults are overwritten from other properties in the array.
130         *
131         * @var Settings_Section_Element_Abstract[] $fields Each element to be displayed.
132         */
133        $fields = array();
134
135        $fields[] = new Enable_Magic_Links( $settings_page_slug_name, $this->settings );
136        $fields[] = new Expiry_Age( $settings_page_slug_name, $this->settings );
137        $fields[] = new Admin_Enable( $settings_page_slug_name, $this->settings );
138        $fields[] = new Use_WP_Login( $settings_page_slug_name, $this->settings );
139        $fields[] = new Klaviyo_Private_Key( $settings_page_slug_name, $this->settings );
140        $fields[] = new Log_Level( $settings_page_slug_name, $this->settings );
141
142        foreach ( $fields as $field ) {
143
144            $field->add_settings_field();
145
146            $field->register_setting();
147
148        }
149    }
150}