Completed
Pull Request — componentlibrary (#307)
by
unknown
03:17 queued 01:43
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
    // die();
40
41
    add_rewrite_rule("{$routeName}/?(.*?)/?$", "index.php?{$routeName}=\$matches[1]", "top");
42
    add_rewrite_tag("%{$routeName}%", "([^&]+)");
43
44
    $rules = get_option('rewrite_rules');
45
46
    if (! isset($rules["{$routeName}/(.*?)/?$"])) {
47
        flush_rewrite_rules();
48
    }
49
}
50
51
function templateInclude($template)
52
{
53
    $routeName = ROUTENAME;
0 ignored issues
show
Unused Code introduced by
$routeName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
    global $wp_query;
55
56
    // var_dump($wp_query->query_vars);die();
57
58
    if (isset($wp_query->query_vars['BaseStyle'])) {
59
        return get_template_directory() . '/templates/basestyle.php';
60
    }
61
62
    return $template;
63
}
64