Passed
Push — master ( 410349...4e2bae )
by litefeel
02:24
created

Writing_On_GitHub_Admin::settings_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Administrative UI views and callbacks
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Admin
9
 */
10
class Writing_On_GitHub_Admin {
11
12
    /**
13
     * plugin file name rel plugin dir.
14
     * @var string
15
     */
16
    protected $plugin_file;
17
18
    /**
19
     * Hook into GitHub API
20
     */
21
    public function __construct( $plugin_file ) {
22
        $this->plugin_file = $plugin_file;
23
24
        add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
25
        add_action( 'admin_init', array( $this, 'register_settings' ) );
26
        add_action( 'current_screen', array( $this, 'trigger_cron' ) );
27
        add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 );
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
28
    }
29
30
    /**
31
     * settings link
32
     * @param  string[] $links
33
     * @param  string $file
34
     * @return string[]
35
     */
36
    public function settings_links( $links, $file ) {
37
        if ( $file != $this->plugin_file ) {
38
            return $links;
39
        }
40
41
        $settings_link = '<a href="options-general.php?page=' .
42
        Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>';
43
44
        array_push( $links, $settings_link );
45
46
        return $links;
47
    }
48
49
    /**
50
     * Callback to render the settings page view
51
     */
52
    public function settings_page() {
53
        include dirname( dirname( __FILE__ ) ) . '/views/options.php';
54
    }
55
56
    /**
57
     * Callback to register the plugin's options
58
     */
59
    public function register_settings() {
60
        add_settings_section(
61
            'general',
62
            'General Settings',
63
            array( $this, 'section_callback' ),
64
            Writing_On_GitHub::$text_domain
65
        );
66
67
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' );
68
        add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
69
                'default'   => 'https://api.github.com',
70
                'name'      => 'wogh_host',
71
                'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ),
72
            )
73
        );
74
75
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' );
76
        add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
77
                'default'   => '',
78
                'name'      => 'wogh_repository',
79
                'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ),
80
            )
81
        );
82
83
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' );
84
        add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
85
                'default'   => 'master',
86
                'name'      => 'wogh_branch',
87
                'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ),
88
            )
89
        );
90
91
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' );
92
        add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
93
                'default'   => '',
94
                'name'      => 'wogh_oauth_token',
95
                'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ),
96
            )
97
        );
98
99
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' );
100
        add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
101
                'default'   => '',
102
                'name'      => 'wogh_secret',
103
                'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ),
104
            )
105
        );
106
107
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' );
108
        add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
109
                'default'   => '',
110
                'name'      => 'wogh_default_user',
111
                'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ),
112
            )
113
        );
114
115
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' );
116
        add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
117
                'default'   => '',
118
                'name'      => 'wogh_ignore_author',
119
                'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ),
120
            )
121
        );
122
123
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' );
124
        add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
125
                'default'   => '',
126
                'name'      => 'wogh_dont_export_content',
127
                'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ),
128
            )
129
        );
130
131
        // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' );
132
        // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
133
        //      'default'   => '',
134
        //      'name'      => 'wogh_ignore_metas',
135
        //      'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ),
136
        //  )
137
        // );
138
    }
139
140
    /**
141
     * Callback to render an individual options field
142
     *
143
     * @param array $args Field arguments.
144
     */
145
    public function field_callback( $args ) {
146
        include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php';
147
    }
148
149
    /**
150
     * Callback to render the default import user field.
151
     *
152
     * @param array $args Field arguments.
153
     */
154
    public function user_field_callback( $args ) {
155
        include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php';
156
    }
157
158
    /**
159
     * Callback to render the textarea field.
160
     *
161
     * @param array $args Field arguments.
162
     */
163
    public function textarea_field_callback( $args ) {
164
        include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php';
165
    }
166
167
    /**
168
     * Callback to render the checkbox field.
169
     *
170
     * @param array $args Field arguments.
171
     */
172
    public function checkbox_field_callback( $args ) {
173
        include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php';
174
    }
175
176
    /**
177
     * Displays settings messages from background processes
178
     */
179
    public function section_callback() {
180
        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
181
            return;
182
        }
183
184
        if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?>
185
            <div class="updated">
186
                <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p>
187
            </div><?php
188
            delete_option( '_wogh_export_started' );
189
        }
190
191 View Code Duplication
        if ( $message = get_option( '_wogh_export_error' ) ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
            <div class="error">
193
                <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p>
194
            </div><?php
195
            delete_option( '_wogh_export_error' );
196
        }
197
198
        if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?>
199
            <div class="updated">
200
                <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p>
201
            </div><?php
202
            delete_option( '_wogh_export_complete' );
203
        }
204
205
        if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?>
206
            <div class="updated">
207
            <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p>
208
            </div><?php
209
            delete_option( '_wogh_import_started' );
210
        }
211
212 View Code Duplication
        if ( $message = get_option( '_wogh_import_error' ) ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
            <div class="error">
214
            <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p>
215
            </div><?php
216
            delete_option( '_wogh_import_error' );
217
        }
218
219
        if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?>
220
            <div class="updated">
221
            <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p>
222
            </div><?php
223
            delete_option( '_wogh_import_complete' );
224
        }
225
    }
226
227
    /**
228
     * Add options menu to admin navbar
229
     */
230
    public function add_admin_menu() {
231
        add_options_page(
232
            __( 'Writing On GitHub', 'writing-on-github' ),
233
            __( 'Writing On GitHub', 'writing-on-github' ),
234
            'manage_options',
235
            Writing_On_GitHub::$text_domain,
236
            array( $this, 'settings_page' )
237
        );
238
    }
239
240
    /**
241
     * Admin callback to trigger import/export because WordPress admin routing lol
242
     */
243
    public function trigger_cron() {
244
        if ( ! current_user_can( 'manage_options' ) ) {
245
            return;
246
        }
247
248
        if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
249
            return;
250
        }
251
252
        if ( ! isset( $_GET['action'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
253
            return;
254
        }
255
256
        if ( 'export' === $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
257
            Writing_On_GitHub::$instance->start_export();
258
        }
259
260
        if ( 'force_export' === $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
261
            Writing_On_GitHub::$instance->start_export(true);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
262
        }
263
264
        if ( 'import' === $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
265
            Writing_On_GitHub::$instance->start_import();
266
        }
267
268
        wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) );
269
        die;
270
    }
271
}
272