-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
100 lines (73 loc) · 3.08 KB
/
plugin.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
Plugin Name: Case insensitive YOURLS
Plugin URI: https://github.com/IMExperts/yourls-case-insensitive
Description: Makes YOURLS case insensitive
Version: 1.1
Author: IMExperts
Author URI: http://theseotools.net
YOURLS Version: 1.9+
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Hook our custom function into the 'add_new_link' filter
yourls_add_filter( 'shunt_keyword_is_taken', 'insensitive_keyword_is_taken' );
yourls_add_filter( 'shunt_get_keyword_info', 'insensitive_get_keyword_info' );
yourls_add_filter( 'shunt_update_clicks', 'insensitive_update_clicks' );
// If the keyword exists, display the long URL in the error message
function insensitive_keyword_is_taken( $return, $keyword ) {
$ydb = yourls_get_db();
$keyword = yourls_sanitize_keyword( $keyword );
$taken = false;
$table = YOURLS_DB_TABLE_URL;
$already_exists = $ydb->fetchObjects( "SELECT count(*) as cnt FROM `$table` WHERE LOWER(`keyword`) = LOWER('$keyword');" );
$value = json_decode(json_encode($already_exists), true);
if ( $value[0]["cnt"] == 0 )
{
$taken = false;
}
else{
$taken = true;
}
return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
}
function insensitive_get_keyword_infos( $keyword, $use_cache = true ) {
$ydb = yourls_get_db();
$keyword = yourls_sanitize_keyword( $keyword );
yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
if( $ydb->has_infos($keyword) && $use_cache === true ) {
return yourls_apply_filter( 'get_keyword_infos', $ydb->get_infos($keyword), $keyword );
}
yourls_do_action( 'get_keyword_not_cached', $keyword );
$table = YOURLS_DB_TABLE_URL;
$infos = $ydb->fetchObject("SELECT * FROM `$table` WHERE LOWER(`keyword`) = LOWER('$keyword')");
if( $infos ) {
$infos = (array)$infos;
$ydb->set_infos($keyword, $infos);
} else {
// is NULL if not found
$infos = false;
$ydb->set_infos($keyword, false);
}
return yourls_apply_filter( 'get_keyword_infos', $infos, $keyword );
}
function insensitive_get_keyword_info( $return, $keyword, $field, $notfound ) {
$keyword = yourls_sanitize_keyword( $keyword );
$infos = insensitive_get_keyword_infos( $keyword );
$return = $notfound;
if ( isset( $infos[ $field ] ) && $infos[ $field ] !== false )
$return = $infos[ $field ];
return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
}
function insensitive_update_clicks( $return, $keyword, $clicks = false ) {
$ydb = yourls_get_db();
$keyword = yourls_sanitize_keyword( $keyword );
$table = YOURLS_DB_TABLE_URL;
error_log(var_export($keyword, true));
if ( $clicks !== false && is_int( $clicks ) && $clicks >= 0 )
$update = $ydb->fetchAffected( "UPDATE `$table` SET `clicks` = :clicks WHERE LOWER(`keyword`) = LOWER(:keyword)", array('clicks' => $clicks, 'keyword' => $keyword) );
else
$update = $ydb->fetchAffected( "UPDATE `$table` SET `clicks` = clicks + 1 WHERE LOWER(`keyword`) = LOWER(:keyword)", array('keyword' => $keyword) );
yourls_do_action( 'update_clicks', $keyword, $update, $clicks );
return $update;
}