-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpit-utility.php
More file actions
executable file
·195 lines (155 loc) · 4.71 KB
/
wpit-utility.php
File metadata and controls
executable file
·195 lines (155 loc) · 4.71 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
Plugin Name: WPIT Utility
Description: Here goes all custom utility
Author: Wolly, xlthlx, flodolo
Version: 1.0
*/
define ( 'WPIT_UTILITY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define ( 'WPIT_UTILITY_PLUGIN_DIR', plugin_dir_url( __FILE__ ) );
define ( 'WPIT_UTILITY_PLUGIN_SLUG', basename( dirname( __FILE__ ) ) );
define ( 'WPIT_UTILITY_PLUGIN_VERSION', '1.0' );
define ( 'WPIT_UTILITY_PLUGIN_VERSION_NAME', 'wpit_utility_version' );
//include option panel
include_once 'inc/class-wpit-utility-options.php';
class Wpit_Utility {
//A static member variable representing the class instance
private static $_instance = null;
private $options;
/**
* Wpit_Utility::__construct()
* Locked down the constructor, therefore the class cannot be externally instantiated
*
* @param array $args various params some overidden by default
*
* @return
*/
private function __construct() {
//get options
$this->options = get_option( 'wpit-options' );
// init actions (update)...
add_action( 'init', array( $this, 'update_check' ) );
//Add GA script in footer
if ( 'footer' != $this->options['footer'] ){
add_action( 'wp_head', array( $this, 'add_googleanalytics' ) );
} else {
add_action( 'wp_footer', array( $this, 'add_googleanalytics' ) );
}
// Use youtube-nocookies for oEmbed of YouTube videos
add_filter( 'embed_oembed_html', 'youtube_oembed_nocookie', 10, 4 );
//remove WordPress capitalization
remove_filter( 'the_title', 'capital_P_dangit', 11 );
remove_filter( 'the_content', 'capital_P_dangit', 11 );
remove_filter( 'comment_text', 'capital_P_dangit', 31 );
// Shortcodes...
add_shortcode( 'placeholder', array( $this, 'placeholder_creator' ) );
}
/**
* Wpit_Utility::__clone()
* Prevent any object or instance of that class to be cloned
*
* @return
*/
public function __clone() {
trigger_error( "Cannot clone instance of Singleton pattern ...", E_USER_ERROR );
}
/**
* Wpit_Utility::__wakeup()
* Prevent any object or instance to be deserialized
*
* @return
*/
public function __wakeup() {
trigger_error( 'Cannot deserialize instance of Singleton pattern ...', E_USER_ERROR );
}
/**
* Wpit_Utility::getInstance()
* Have a single globally accessible static method
*
* @param mixed $args
*
* @return
*/
public static function getInstance( $args = array() ) {
if ( ! is_object( self::$_instance ) )
self::$_instance = new self( $args );
return self::$_instance;
}
/**
* update_UTILITY_check function.
*
* @access public
* @return void
*/
public function update_check() {
// Do checks only in backend
if ( is_admin() ) {
if ( version_compare( get_site_option( WPIT_UTILITY_VERSION_NAME ), WPIT_UTILITY_VERSION ) != 0 ) {
$this->do_update();
}
} //end if only in the admin
}
/**
* do_update function.
*
* @access private
*
*/
private function do_update(){
//DO NOTHING, BY NOW, MAYBE IN THE FUTURE
//Update option
update_option( WPIT_UTILITY_VERSION_NAME , WPIT_UTILITY_VERSION_NUMBER_VERSION );
}
/**
* add_googleanalytics function.
*
* insert the GA script
*
* @access public
*
*/
public function add_googleanalytics(){
?>
<!-- GA code inserted by WPIT Utility plugin-->
<?php echo $this->options['ga_ua']; ?>
<!-- END GA code inserted by WPIT Utility plugin-->
<?php
}
/**
* youtube_oembed_nocookie function.
*
* @access public
* @param string $html Google Video HTML embed markup.
* @param string $url The attempted embed URL.
* @param array $attr An array of shortcode attributes.
* @param int $post_ID Post ID.
*
* @return $content
*/
function youtube_oembed_nocookie($html, $url, $attr, $post_id) {
$search = '/youtube\.com/';
$replace = 'youtube-nocookie.com';
return preg_replace( $search, $replace, $html );
}
/**
* Shortcode for a nice image placeholder
* @author Napolux <napolux@gmail.com>
* @usage [placeholder width=300 height=400 secure=true]
*/
public function placeholder_creator($attr = array()) {
// Default values, if no values are passed
if(!is_array($attr) || empty($attr)) {
$secure = true;
$width = 200;
$height = 200;
} else {
$secure = ($attr['secure'] === 'true') ? true : false;
$width = (int) $attr['width'];
$height = (int) $attr['height'];
}
$imgUrl = '<img title="placeholder" alt="placeholder" src="' . (($secure) ? 'https://' : 'http://') . 'placehold.it/' . $width . 'x' . $height . '" />';
return $imgUrl;
}
}// end class
//Class instance
$wpit_utility = Wpit_Utility::getInstance();