Completed
Push — beta ( da515e...b40428 )
by
unknown
06:35
created

Core::add_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 *
4
 *
5
 * inspired by https://github.com/adamsilverstein/wp-post-meta-revisions/blob/master/wp-post-meta-revisions.php
6
 * many thx @adamsilverstein
7
 *
8
 */
9
10
namespace KMM\Timeshift;
11
12
class Core {
13
    private $plugin_dir;
14
    private $last_author = false;
15
    private $timeshift_cached_meta;
16
17 11
    public function __construct($i18n) {
18
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
19 11
        $this->i18n = $i18n;
0 ignored issues
show
Bug introduced by
The property i18n does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20 11
        $this->wpdb = $wpdb;
0 ignored issues
show
Bug introduced by
The property wpdb does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21 11
        $this->plugin_dir = plugin_dir_url(__FILE__) . '../';
22 11
        $this->timeshift_posts_per_page = 5;
0 ignored issues
show
Bug introduced by
The property timeshift_posts_per_page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23 11
        $this->pagination_ajax_action = 'pagination_timeshift';
0 ignored issues
show
Bug introduced by
The property pagination_ajax_action does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 11
        $this->add_filters();
25 11
        $this->add_actions();
26 11
        $this->add_metabox();
27
        // Disable WP's own revision system
28 11
        remove_post_type_support('post', 'revisions');
29
    }
30
31 2
    public function hasTimeshifts($post_id) {
32 2
        $post_type = get_post_type($post_id);
33 2
        $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type;
34 2
        $this->checkTable($post_type);
35 2
        $sql = "select count(1) as amount from $table_name where post_id=" . $post_id;
36 2
        $r = $this->wpdb->get_results($sql);
37
38 2
        if ($r && 1 == count($r)) {
39 1
            if (intval($r[0]->amount) > 0) {
40 1
                return true;
41
            }
42
        }
43
44 1
        return false;
45
    }
46
47 11
    public function timeshiftVisible() {
48 11
        $check = apply_filters('krn_timeshift_visible', true);
49
50 11
        return $check;
51
    }
52
53 11
    public function add_metabox() {
54 11
        $cl = $this;
55 11
        if (! $this->timeshiftVisible()) {
56
            return;
57
        }
58 11
        if (! isset($_GET['post']) || ! $this->hasTimeshifts($_GET['post'])) {
59 11
            return;
60
        }
61
        add_action('add_meta_boxes', function () use ($cl) {
62
            add_meta_box('krn-timeshift', __('Timeshift', 'kmm-timeshift'), [$cl, 'timeshift_metabox'], null, 'normal', 'core');
63
        });
64
    }
65
66 1
    public function timeshift_metabox() {
67 1
        if (! isset($_GET['post'])) {
68 1
            return;
69
        }
70
        $prod_post = get_post($_GET['post']);
71
72
        $start = 0;
73
        $timeshift_page = 1;
74
        if (isset($_GET['timeshift_page'])) {
75
            $start = ($_GET['timeshift_page'] - 1) * $this->timeshift_posts_per_page;
76
            $timeshift_page = $_GET['timeshift_page'];
77
        }
78
79
        // pagination
80
        $pagination = $this->get_paginated_links($prod_post, $timeshift_page);
81
        echo $pagination;
82
83
        // load first few & render
84
        $rows = $this->get_next_rows($prod_post, $start);
85
        $timeshift_table = $this->render_metabox_table($prod_post, $rows);
86
        echo $timeshift_table;
87
88
        if (isset($_GET['action']) && $_GET['action'] == $this->pagination_ajax_action) {
89
            wp_die();
90
        }
91
    }
92
93 11
    public function add_filters() {
94
        // When revisioned post meta has changed, trigger a revision save.
95
        //add_filter('wp_save_post_revision_post_has_changed', [$this, '_wp_check_revisioned_meta_fields_have_changed'], 10, 3);
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
97 11
        add_filter('get_post_metadata', [$this, 'inject_metadata_timeshift'], 1, 4);
98 11
        add_filter('update_post_metadata', [$this, 'update_post_metadata'], 1, 5);
99
    }
100
101 1
    public function update_post_metadata($check, int $object_id, string $meta_key, $meta_value, $prev_value) {
0 ignored issues
show
Unused Code introduced by
The parameter $meta_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $prev_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102 1
        if ('_edit_last' == $meta_key) {
103 1
            $lo = get_post_meta($object_id, '_edit_last', true);
104 1
            $this->last_author = $lo;
105
        }
106
107 1
        return null;
108
    }
109
110 2
    public function inject_metadata_timeshift($value, $post_id, $key, $single) {
0 ignored issues
show
Unused Code introduced by
The parameter $single is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111 2
        if (! isset($_GET['timeshift'])) {
112 2
            return;
113
        }
114
        // Load timeshift
115
        if (! $this->timeshift_cached_meta) {
116
            $post_type = get_post_type($post_id);
117
            $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type;
118
            $sql = "select * from $table_name where id=" . intval($_GET['timeshift']);
119
            $r = $this->wpdb->get_results($sql);
120 View Code Duplication
            if ($r && 1 == count($r)) {
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...
121
                $payload = unserialize($r[0]->post_payload);
122
                $this->timeshift_cached_meta = $payload->meta;
123
            }
124
        }
125
        // is the requested meta data in the stored snapshot
126
        if ($this->timeshift_cached_meta && isset($this->timeshift_cached_meta[$key])) {
127
            return $this->timeshift_cached_meta[$key];
128
        } else {
129
            // Otherwise return default value, like acf core fields.
130
            return $value;
131
        }
132
    }
133
134 2
    public function inject_timeshift($p) {
0 ignored issues
show
Unused Code introduced by
The parameter $p is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
        global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
136 2
        if (! isset($_GET['timeshift'])) {
137 1
            return;
138
        }
139
        // Load timeshift
140 1
        $table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type;
141 1
        $sql = "select * from $table_name where id=" . intval($_GET['timeshift']);
142 1
        $r = $this->wpdb->get_results($sql);
143 1 View Code Duplication
        if ($r && 1 == count($r)) {
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...
144
            $payload = unserialize($r[0]->post_payload);
145
            $post = $payload->post;
146
        }
147
    }
148
149 11
    public function add_actions() {
150 11
        add_action('edit_form_top', [$this, 'inject_timeshift'], 1, 1);
151 11
        add_action('pre_post_update', [$this, 'pre_post_update'], 2, 1);
152 11
        add_action('admin_notices', [$this, 'admin_notice']);
153 11
        add_action('krn_timeshift_create_snapshot', [$this, 'create_snapshot'], 1, 1);
154 11
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
155 11
        add_action('wp_ajax_pagination_timeshift', [$this, 'timeshift_metabox']);
156
    }
157
158
    public function admin_notice() {
159
        if (isset($_GET['timeshift']) && $_GET['timeshift']) {
160
            echo '<div class="notice notice-warning is-dismissible">';
161
            echo '<p style="font-weight: 800; color: red">' . __('You are editing a historical version! if you save or publish, this will replace the current live one', 'kmm-timeshift') . '</p>';
162
            echo '</div>';
163
        }
164
    }
165
166
    public function enqueue_scripts() {
167
        wp_enqueue_script('krn-timeshift-pagination-ajax', plugin_dir_url(__FILE__) . '../assets/js/pagination-ajax.js', ['jquery']);
168
        wp_localize_script('krn-timeshift-pagination-ajax', 'krn_timeshift', [
169
            'action' => $this->pagination_ajax_action,
170
            'post' => isset($_GET['post']) ? $_GET['post'] : false,
171
            'timeshift' => isset($_GET['timeshift']) ? $_GET['timeshift'] : false,
172
        ]);
173
    }
174
175 4
    public function checkTable($postType) {
176 4
        $table_name = $this->wpdb->prefix . 'timeshift_' . $postType;
177
178 4
        $charset_collate = $this->wpdb->get_charset_collate();
179
180 4
        $sql = "CREATE TABLE IF NOT EXISTS $table_name (
181
                id int(12) NOT NULL AUTO_INCREMENT,
182
                post_id int(12) NOT NULL,
183
                create_date datetime default CURRENT_TIMESTAMP,
184
                post_payload LONGTEXT,
185
                PRIMARY KEY (id)
186 4
            ) $charset_collate;";
187
188 4
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
189 4
        $a = dbDelta($sql);
0 ignored issues
show
Unused Code introduced by
$a 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...
190
191 4
        return true;
192
    }
193
194 2
    public function storeTimeshift($timeshift) {
195 2
        $table_name = $this->wpdb->prefix . 'timeshift_' . $timeshift->post->post_type;
196 2
        $sql = "insert into $table_name (post_id, post_payload) VALUES(%d, '%s')";
197 2
        $query = $this->wpdb->prepare($sql, $timeshift->post->ID, serialize($timeshift));
198 2
        $this->wpdb->query($query);
199
    }
200
201
    public function create_snapshot($postID) {
202
        $this->pre_post_update($postID);
203
    }
204
205 1
    public function pre_post_update(int $post_ID, array $data = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206 1
        if (wp_is_post_autosave($post_ID)) {
207
            return;
208
        }
209 1
        if ('auto-draft' == get_post_status($post_ID)) {
210
            return;
211
        }
212 1
        $post_type = get_post_type($post_ID);
213 1
        $this->checkTable($post_type);
214
215 1
        $mdata = get_metadata('post', $post_ID);
216 1
        $post = get_post($post_ID);
217
218 1
        if ($this->last_author) {
219
            $mdata['_edit_last'][0] = $this->last_author;
220
        }
221 1
        unset($mdata['_edit_lock']);
222
223 1
        $timeshift = (object) ['post' => $post, 'meta' => $mdata];
224 1
        $this->storeTimeshift($timeshift);
225
    }
226
227
    public function get_paginated_links($prod_post, $paged = 1) {
228
        if (is_null($prod_post)) {
229
            return;
230
        }
231
232
        // count timeshift-versions
233
        $table_name = $this->wpdb->prefix . 'timeshift_' . $prod_post->post_type;
234
        $sql = "select  count(1) as cnt from $table_name where post_id=" . $prod_post->ID;
235
        $maxrows = $this->wpdb->get_results($sql);
236
        $allrows = (int) $maxrows[0]->{'cnt'};
237
238
        // max. number of pages
239
        $max_page = ceil($allrows / $this->timeshift_posts_per_page);
240
241
        // create pagination links
242
        $output = paginate_links([
243
            'current' => max(1, $paged),
244
            'total' => $max_page,
245
            'mid_size' => 1,
246
            'prev_text' => __('«'),
247
            'next_text' => __('»'),
248
        ]);
249
250
        return $output;
251
    }
252
253
    public function get_next_rows($prod_post, $start = 0) {
254
        if (! isset($prod_post)) {
255
            return;
256
        }
257
258
        $table_name = $this->wpdb->prefix . 'timeshift_' . $prod_post->post_type;
259
        $sql = "select  * from $table_name where post_id=" . $prod_post->ID . ' order by create_date desc limit ' . $start . ', ' . $this->timeshift_posts_per_page;
260
        $rows = $this->wpdb->get_results($sql);
261
262
        return $rows;
263
    }
264
265
    public function render_metabox_table($prod_post, $rows = []) {
266
        if (! isset($prod_post)) {
267
            return;
268
        }
269
270
        // get last editor
271
        $table_postmeta = $this->wpdb->prefix . 'postmeta';
272
        $sql_last_editor = 'select meta_value from ' . $table_postmeta . ' where post_id=' . $prod_post->ID . " AND meta_key='_edit_last'";
273
        $last_editor = $this->wpdb->get_var($sql_last_editor);
274
275
        $output = '<table class="widefat fixed">';
276
        $output .= '<thead>';
277
        $output .= '<tr>';
278
        $output .= '<th width=30></th>';
279
        $output .= '<th width="40%" id="columnname" class="manage-column column-columnname"  scope="col">' . __('Title', 'kmm-timeshift') . '</th>';
280
        $output .= '<th width="30%" id="columnname" class="manage-column column-columnname"  scope="col">' . __('Snapshot Date', 'kmm-timeshift') . '</th>';
281
        $output .= '<th width="10%" id="columnname" class="manage-column column-columnname"  scope="col">' . __('Author', 'kmm-timeshift') . '</th>';
282
        $output .= '<th width="10%" id="columnname" class="manage-column column-columnname"  scope="col">' . __('Actions', 'kmm-timeshift') . '</th>';
283
        $output .= '</tr>';
284
        $output .= '</thead>';
285
        $output .= '<tbody>';
286
287
        // live-version
288
        $output .= '<tr style="font-weight: 800;">';
289
        $output .= '<td>' . get_avatar($last_editor, 30) . '</td>';
290
        $output .= '<td>' . $prod_post->post_title . '</td>';
291
        $output .= '<td>' . $prod_post->post_modified . '</td>';
292
        $output .= '<td>' . get_the_author_meta('display_name', $last_editor) . '</td>';
293
        $output .= '<td><a href="post.php?post=' . $_GET['post'] . '&action=edit"><span class="dashicons dashicons-admin-site"></span></A></td>';
294
        $output .= '</tr>';
295
296
        foreach ($rows as $rev) {
297
            $timeshift = unserialize($rev->post_payload);
298
            $style = '';
299
300
            // highlight currently loaded version
301
            if (isset($_GET['timeshift']) && $_GET['timeshift'] == $rev->id) {
302
                $style = 'style="font-style:italic;background-color: lightblue;"';
303
            }
304
305
            // some images dont have _edit_last field
306
            if (! isset($timeshift->meta['_edit_last']) || is_null($timeshift->meta['_edit_last'])) {
307
                $timeshift->meta['_edit_last'] = 0;
308
            }
309
310
            $output .= '<tr ' . $style . '>';
311
            $output .= '<td>' . get_avatar($timeshift->meta['_edit_last'][0], 30) . '</td>';
312
            $output .= '<td>' . $timeshift->post->post_title . '</td>';
313
            $output .= '<td>' . $timeshift->post->post_modified . '</td>';
314
            $output .= '<td>' . get_the_author_meta('display_name', $timeshift->meta['_edit_last'][0]) . '</td>';
315
            $output .= '<td><a href="post.php?post=' . $_GET['post'] . '&action=edit&timeshift=' . $rev->id . '"><span class="dashicons dashicons-backup"></span></a></td>';
316
            $output .= '</tr>';
317
        }
318
319
        $output .= '</tbody>';
320
        $output .= '</table>';
321
322
        return $output;
323
    }
324
}
325