Completed
Push — componentlibrary ( 67a692...eed31d )
by Doğa
01:48
created

baseStyle.php ➔ disallowRobots()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Loads a `basestyle.php` template from the templates folder to render base style markup for proper base styling, if user is on non-production environment or has edit rights at least.
5
 *
6
 * Example usage:
7
 * 1. Log into your WordPress Backend with an Administrator account.
8
 * 2. Navigate your browser to `/BaseStyle/`.
9
 */
10
11
namespace Flynt\BaseStyle;
12
13
if (current_user_can('editor') || (WP_ENV !== 'production')) {
14
    add_filter('init', 'Flynt\BaseStyle\registerRewriteRule');
15
    add_filter('template_include', 'Flynt\BaseStyle\templateInclude');
16
    disallowRobots();
17
}
18
19
const ROUTENAME = 'BaseStyle';
20
21
function disallowRobots()
22
{
23
    if (defined('WPSEO_VERSION')) {
24
        add_filter('wpseo_robots', function () {
25
            return "noindex,nofollow";
26
        });
27
    } else {
28
        remove_action('wp_head', 'noindex', 1);
29
        add_action('wp_head', function () {
30
            echo "<meta name='robots' content='noindex,nofollow' />\n";
31
        });
32
    }
33
}
34
35
function registerRewriteRule()
36
{
37
    $routeName = ROUTENAME;
38
39
    add_rewrite_rule("{$routeName}/?(.*?)/?$", "index.php?{$routeName}=\$matches[1]", "top");
40
    add_rewrite_tag("%{$routeName}%", "([^&]+)");
41
42
    $rules = get_option('rewrite_rules');
43
44
    if (! isset($rules["{$routeName}/(.*?)/?$"])) {
45
        flush_rewrite_rules();
46
    }
47
}
48
49
function templateInclude($template)
50
{
51
    global $wp_query;
52
53
    if (isset($wp_query->query_vars['BaseStyle'])) {
54
        return get_template_directory() . '/templates/basestyle.php';
55
    }
56
57
    return $template;
58
}
59