Passed
Branch master (6aefea)
by litefeel
02:56
created

Writing_On_GitHub_Admin::trigger_cron()   C

Complexity

Conditions 8
Paths 19

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 19
nop 0
dl 0
loc 28
ccs 0
cts 21
cp 0
crap 72
rs 5.3846
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' ) );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
Loading history...
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...
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        /** @scrutinizer ignore-call */ 
28
        add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 );
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>';
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        Writing_On_GitHub::$text_domain . '">' . /** @scrutinizer ignore-call */ __( 'Settings', 'writing-on-github' ) . '</a>';
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        /** @scrutinizer ignore-call */ 
61
        add_settings_section(
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        /** @scrutinizer ignore-call */ 
68
        register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' );
Loading history...
68
        add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        add_settings_field( 'wogh_host', /** @scrutinizer ignore-call */ __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
Loading history...
Bug introduced by
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        /** @scrutinizer ignore-call */ 
69
        add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array(
Loading history...
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...
Bug introduced by
The function get_current_screen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
        if ( /** @scrutinizer ignore-call */ get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) {
Loading history...
181
            return;
182
        }
183
184
        if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?>
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        if ( 'yes' === /** @scrutinizer ignore-call */ get_option( '_wogh_export_started' ) ) { ?>
Loading history...
185
            <div class="updated">
186
                <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p>
0 ignored issues
show
Bug introduced by
The function esc_html_e was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
                <p><?php /** @scrutinizer ignore-call */ esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p>
Loading history...
187
            </div><?php
188
            delete_option( '_wogh_export_started' );
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
            /** @scrutinizer ignore-call */ 
189
            delete_option( '_wogh_export_started' );
Loading history...
189
        }
190
191
        if ( $message = get_option( '_wogh_export_error' ) ) { ?>
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>
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
                <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo /** @scrutinizer ignore-call */ esc_html( $message );?></p>
Loading history...
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
        if ( $message = get_option( '_wogh_import_error' ) ) { ?>
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(
0 ignored issues
show
Bug introduced by
The function add_options_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
        /** @scrutinizer ignore-call */ 
232
        add_options_page(
Loading history...
232
            __( 'Writing On GitHub', 'writing-on-github' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
            /** @scrutinizer ignore-call */ 
233
            __( 'Writing On GitHub', 'writing-on-github' ),
Loading history...
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' ) ) {
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

244
        if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_options' ) ) {
Loading history...
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...
Bug introduced by
The function get_current_screen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
        if ( /** @scrutinizer ignore-call */ get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) {
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
        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...
260
            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...
261
        }
262
        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...
263
            Writing_On_GitHub::$instance->start_import();
264
        }
265
        if ( 'force_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...
266
            Writing_On_GitHub::$instance->start_import(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...
267
        }
268
269
        wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) );
0 ignored issues
show
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
        wp_redirect( /** @scrutinizer ignore-call */ admin_url( 'options-general.php?page=writing-on-github' ) );
Loading history...
Bug introduced by
The function wp_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
        /** @scrutinizer ignore-call */ 
270
        wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) );
Loading history...
270
        die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
271
    }
272
}
273