summaryrefslogtreecommitdiffstats
path: root/includes/class-giglogadmin-plugin.php
blob: 96f3c5fd0dc6b413c2300d0ee8a3fea1cff6f2da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 * The main giglogadmin plugin code.
 *
 * @package giglogadmin
 *
 * SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
 * SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

if ( ! class_exists( 'GiglogAdmin_Plugin' ) ) {

    /**
     * The Main GiglogAdmin plugin class.
     *
     * Handles initialization and loads the rest of the plugin as needed.
     */
    class GiglogAdmin_Plugin {

        /**
         * Initialize the plugin.
         *
         * Loads the required modules, and registers classbacks for
         * adding the admin pages and filters.
         */
        public static function init(): void {
            if ( ! defined( 'GIGLOGADMIN_UNIT_TEST' ) ) {
                require_once __DIR__ . '/giglogadmin-register-db-tables.php';
                giglog_register_db_tables();
            }

            require_once __DIR__ . '/class-giglogadmin-venue.php';
            require_once __DIR__ . '/class-giglogadmin-concert.php';
            require_once __DIR__ . '/class-giglogadmin-icalexport.php';
            require_once __DIR__ . '/view-helpers/select-field-helper.php';

            if ( is_admin() ) {
                require_once __DIR__ . '/admin/views/class-giglogadmin-adminpage.php';
                require_once __DIR__ . '/admin/views/class-giglogadmin-importgigspage.php';
                require_once __DIR__ . '/admin/helpfiles/instrunctions.php';
                require_once __DIR__ . '/admin/helpfiles/instr_reviewers.php';
                require_once __DIR__ . '/admin/helpfiles/instr_photog.php';

                add_action( 'admin_menu', array( 'GiglogAdmin_Plugin', 'add_admin_pages' ) );
                add_action( 'admin_menu', array( 'GiglogAdmin_Plugin', 'add_help_pages' ) );

                add_filter( 'wp_nav_menu_args', array( 'GiglogAdmin_Plugin', 'nav_menu_args' ) );
            } else {
                require_once __DIR__ . '/view-helpers/class-giglogadmin-concertstable.php';
                require_once __DIR__ . '/giglogadmin-shortcodes.php';
            }
        }

        /**
         * Adds the 'Giglog' top level menu to the left side WordPress admin
         * menu. Other subpages will come later.
         *
         * @return void
         */
        public static function add_admin_pages(): void {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
            $top = add_menu_page(
                'Giglog admin',             // Page title
                'Giglog',                   // Menu title
                'upload_files',             // Will show for users with this capability
                'giglog',                   // menu slug
                array( 'GiglogAdmin_AdminPage', 'render_html' ),     // callable
                'dashicons-tickets-alt',    // Icon url
                11                          // Position, just below 'Media'
            );

            add_action( 'load-' . $top, array( 'GiglogAdmin_AdminPage', 'update' ) );

            $import_hook = add_submenu_page(
                'giglog',                   // parent slug
                'Import gigs',              // page title
                'Import gigs',              // menu title
                'upload_files',             // required capability
                'giglog_import',            // menu slug
                array( 'GiglogAdmin_ImportGigsPage', 'render_html' )   // callable
            );

            if ( $import_hook !== false ) {
                add_action(
                    'load-' . $import_hook,
                    array( 'GiglogAdmin_ImportGigsPage', 'submit_form' )
                );
            }

            wp_register_style( 'css_style', plugins_url( '/css/main.css', __FILE__ ), array(), '1' );
            wp_enqueue_style( 'css_style' );
        }

        /**
         * Adds the "help" pages in the WordPress admin menu.
         */
        public static function add_help_pages(): void {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
            add_menu_page(
                'Help for ET users',        // Page title
                'Help for ET users',        // Menu title
                'upload_files',             // Will show for users with this capability
                'helpfiles',                // menu slug
                array( 'Instructions_Page', 'render_instr_html' ),     // callable
                'dashicons-tickets-alt',    // Icon url
                10                          // Position, just below 'Media'
            );

            add_submenu_page(
                'helpfiles',                // parent slug
                'Reviewer help files',      // page title
                'Reviewer help files',      // menu title
                'upload_files',             // required capability
                'reviewer_help',            // menu slug
                array( 'Instructions_Reviewers', 'render_instr_rev_html' )   // callable
            );

            add_submenu_page(
                'helpfiles',                 // parent slug
                'Photogalleries help files', // page title
                'Photogalleries help files', // menu title
                'upload_files',              // required capability
                'photog_help',               // menu slug
                array( 'Instructions_Photogs', 'render_instr_photo_html' ) // callable
            );
        }

        /**
         * Show menus based on whether user is logged in or not.
         *
         * Giglog admin pages should only be visible for logged in users/can eventually
         * be customized by role if needed
         *
         * @param array $args   The menu array to filter.
         */
        private static function nav_menu_args( array $args = array() ) : array {
            if ( is_user_logged_in() ) {
                $args['menu'] = 'Loggedusers';
            } else {
                $args['menu'] = 'Notloggedusers';
            }

            return $args;
        }

    }

    register_activation_hook( __FILE__, array( 'GiglogAdmin_Plugin', 'activate' ) );
    register_deactivation_hook( __FILE__, array( 'GiglogAdmin_Plugin', 'deactivate' ) );

    GiglogAdmin_Plugin::init();
}