Passed
Push — master ( 427312...457a74 )
by Doğa
03:06
created

getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\TimberDynamicResize;
4
5
use Twig_SimpleFilter;
6
use Timber;
7
use Routes;
8
9
const DB_VERSION = '1.0';
10
const TABLE_NAME = 'resized_images';
11
const IMAGE_ROUTE = 'dynamic-images';
12
const IMAGE_PATH_SEPARATOR = 'dynamic';
13
14
function getTableName()
15
{
16
    global $wpdb;
17
    return $wpdb->prefix . TABLE_NAME;
18
}
19
20
call_user_func(function () {
21
    $optionName = TABLE_NAME . '_db_version';
22
23
    $installedVersion = get_option($optionName);
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

23
    $installedVersion = /** @scrutinizer ignore-call */ get_option($optionName);
Loading history...
24
25
    if ($installedVersion !== DB_VERSION) {
26
        global $wpdb;
27
        $tableName = getTableName();
28
29
        $charsetCollate = $wpdb->get_charset_collate();
30
31
        $sql = "CREATE TABLE $tableName (
32
            url varchar(511),
33
            arguments text,
34
            PRIMARY KEY (url)
35
        ) $charsetCollate;";
36
37
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
0 ignored issues
show
Bug introduced by
The constant Flynt\TimberDynamicResize\ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        dbDelta($sql);
0 ignored issues
show
Bug introduced by
The function dbDelta 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

38
        /** @scrutinizer ignore-call */ 
39
        dbDelta($sql);
Loading history...
39
40
        update_option($optionName, DB_VERSION);
0 ignored issues
show
Bug introduced by
The function update_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

40
        /** @scrutinizer ignore-call */ 
41
        update_option($optionName, DB_VERSION);
Loading history...
41
    }
42
});
43
44
add_action('timber/twig/filters', function ($twig) {
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

44
/** @scrutinizer ignore-call */ 
45
add_action('timber/twig/filters', function ($twig) {
Loading history...
45
    $twig->addFilter(new Twig_SimpleFilter('resizeDynamic', function ($src, $w, $h = 0, $crop = 'default', $force = false) {
46
        $resizeOp = new Timber\Image\Operation\Resize($w, $h, $crop);
47
        $fileinfo = pathinfo($src);
48
        $resizedUrl = $resizeOp->filename($fileinfo['dirname'] . '/' . $fileinfo['filename'], $fileinfo['extension']);
49
50
        $arguments = [
51
            'src' => $src,
52
            'w' => $w,
53
            'h' => $h,
54
            'crop' => $crop,
55
            'force' => $force,
56
        ];
57
58
        global $wpdb;
59
        $tableName = getTableName();
60
        $wpdb->query($wpdb->prepare("REPLACE INTO {$tableName} VALUES (%s, %s)", [$resizedUrl, json_encode($arguments)]));
61
62
        return str_replace('/app/uploads/', '/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', $resizedUrl);
63
    }));
64
65
    return $twig;
66
});
67
68
Routes::map(IMAGE_ROUTE, function () {
69
    $src = str_replace('/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', '/app/uploads/', home_url($_GET['src'] ?? ''));
0 ignored issues
show
Bug introduced by
The function home_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

69
    $src = str_replace('/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', '/app/uploads/', /** @scrutinizer ignore-call */ home_url($_GET['src'] ?? ''));
Loading history...
70
71
    global $wpdb;
72
    $tableName = getTableName();
73
    $resizedImage = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$tableName} WHERE url = %s", $src));
74
75
    if (empty($resizedImage)) {
76
        header("HTTP/1.0 404 Not Found");
77
        exit;
78
    }
79
    $urlParts = wp_parse_url($src);
0 ignored issues
show
Bug introduced by
The function wp_parse_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

79
    $urlParts = /** @scrutinizer ignore-call */ wp_parse_url($src);
Loading history...
80
    $homeUrl = home_url();
81
    $localDev = parse_url($homeUrl)['host'] !== $urlParts['host'];
82
    if ($localDev) {
83
        $src = http_build_url($homeUrl, ['path' => $urlParts['path']]);
0 ignored issues
show
Unused Code introduced by
The assignment to $src is dead and can be removed.
Loading history...
84
    }
85
    $moveImageFunction = function ($location) {
86
        return str_replace('/app/uploads/', '/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', $location);
87
    };
88
    add_filter('timber/image/new_url', $moveImageFunction);
0 ignored issues
show
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

88
    /** @scrutinizer ignore-call */ 
89
    add_filter('timber/image/new_url', $moveImageFunction);
Loading history...
89
    add_filter('timber/image/new_path', $moveImageFunction);
90
    $arguments = json_decode($resizedImage->arguments, true);
91
    $url = Timber\ImageHelper::resize($arguments['src'], (int) $arguments['w'], (int) $arguments['h'], $arguments['crop'], false);
92
93
    remove_filter('timber/image/new_url', $moveImageFunction);
0 ignored issues
show
Bug introduced by
The function remove_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

93
    /** @scrutinizer ignore-call */ 
94
    remove_filter('timber/image/new_url', $moveImageFunction);
Loading history...
94
    remove_filter('timber/image/new_path', $moveImageFunction);
95
96
    Timber\ImageHelper::img_to_webp($url);
97
98
    if ($localDev) {
99
        unset($urlParts['path']);
100
        $url = http_build_url($url, $urlParts);
101
    }
102
    header("Location: {$url}", true, 301);
103
    exit;
104
});
105