-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashed-ids.php
78 lines (69 loc) · 2.26 KB
/
hashed-ids.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @package GoldFash Hashed IDs
* @version 2.0
*/
/*
Plugin Name: Hashed-ids
Plugin URL: http://goldfash.com?plugins
Description: Link Hashing and Encrypting
Version: 2.0
Author: GoldFash Design
Author URI: http://goldfash.com/
Contributors: raceanf
Domain Path: /languages
Text Domain: hashed-ids
GitHub Plugin URI: https://github.com/goldfashhosting/hashed-ids
GitHub Branch: master
*/
require_once('hashids/lib/hashids.php-5-3.php');
define ('HASHED_IDS_MIN_LENGTH', 7);
function hashed_id() {
global $wp_rewrite;
add_rewrite_tag('%hashed_id%','([^/]+)');
$permalink = $wp_rewrite->permalink_structure;
if (!empty($permalink) && false !== strpos( $permalink, '%hashed_id%' )) {
add_filter('pre_post_link', '_hashed_id_post_link', 7, 2);
add_filter('parse_request', '_hashed_id_parse_request');
}
}
function _hashed_id_post_link($permalink, $post) {
$hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
$permalink = str_replace('%hashed_id%', $hashids->encrypt((int)$post->ID), $permalink);
return $permalink;
}
function _hashed_id_parse_request($qv) {
$hashed_id = $qv->query_vars['hashed_id'];
if (strlen($hashed_id) > 0) {
$hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
$id = $hashids->decrypt($hashed_id);
if (isset($id[0]) && is_numeric($id[0])) {
$qv->query_vars['p'] = $id[0];
} else {
$qv->query_vars['pagename'] = $hashed_id;
}
}
return $qv;
}
add_action('init', 'hashed_id');
function hashed_ids_activate_plugin() {
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
$wp_rewrite->set_permalink_structure(
str_replace('%post_id%', '%hashed_id%', $wp_rewrite->permalink_structure)
);
}
flush_rewrite_rules(false);
}
register_activation_hook( __FILE__, 'hashed_ids_activate_plugin' );
function hashed_ids_deactivate_plugin() {
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
$wp_rewrite->set_permalink_structure(
str_replace('%hashed_id%', '%post_id%', $wp_rewrite->permalink_structure)
);
}
flush_rewrite_rules(false);
}
register_deactivation_hook( __FILE__, 'hashed_ids_deactivate_plugin' );
?>