-
Notifications
You must be signed in to change notification settings - Fork 9
/
yourls-link-creator.php
executable file
·166 lines (140 loc) · 4.29 KB
/
yourls-link-creator.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
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
<?php
/**
* Plugin Name: YOURLS Link Creator
* Plugin URI: http://andrewnorcross.com/plugins/yourls-link-creator/
* Description: Creates a shortlink using YOURLS and stores as postmeta.
* Author: Andrew Norcross
* Author http://andrewnorcross.com
* Version: 2.1.1
* Text Domain: wpyourls
* Domain Path: languages
* GitHub Plugin URI: https://github.com/norcross/yourls-link-creator
*/
/*
* Copyright 2012 Andrew Norcross
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
if( ! defined( 'YOURLS_BASE' ) ) {
define( 'YOURLS_BASE', plugin_basename(__FILE__) );
}
if( ! defined( 'YOURS_DIR' ) ) {
define( 'YOURS_DIR', plugin_dir_path( __FILE__ ) );
}
if( ! defined( 'YOURS_VER' ) ) {
define( 'YOURS_VER', '2.1.1' );
}
// Start up the engine
class YOURLSCreator
{
/**
* Static property to hold our singleton instance
* @var instance
*/
static $instance = false;
/**
* This is our constructor. There are many like it, but this one is mine.
*
* @return void
*/
private function __construct() {
add_action( 'plugins_loaded', array( $this, 'textdomain' ) );
add_action( 'plugins_loaded', array( $this, 'load_files' ) );
// handle the scheduling and removal of cron jobs
add_action( 'plugins_loaded', array( $this, 'schedule_crons' ) );
register_deactivation_hook ( __FILE__, array( $this, 'remove_crons' ) );
}
/**
* If an instance exists, this returns it. If not, it creates one and
* retuns it.
*
* @return $instance
*/
public static function getInstance() {
// load an instance if not already initalized
if ( ! self::$instance ) {
self::$instance = new self;
}
// return the instance
return self::$instance;
}
/**
* Load textdomain for international goodness.
*
* @return textdomain
*/
public function textdomain() {
load_plugin_textdomain( 'wpyourls', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Call our files in the appropriate place.
*
* @return void
*/
public function load_files() {
// Load our back end.
if ( is_admin() ) {
require_once( 'lib/admin.php' );
require_once( 'lib/settings.php' );
require_once( 'lib/ajax.php' );
}
// Load our front end.
if ( ! is_admin() ) {
require_once( 'lib/front.php' );
}
// Load our global.
require_once( 'lib/global.php' );
// Load our helper file.
require_once( 'lib/helper.php' );
// Load our template tag file.
require_once( 'lib/display.php' );
// Load our legacy file.
require_once( 'lib/legacy.php' );
}
/**
* Add our scheduled cron jobs.
*
* @return void
*/
public function schedule_crons() {
// Optional filter to disable this all together.
if ( false === apply_filters( 'yourls_run_cron_jobs', true ) ) {
return;
}
// Schedule the click check.
if ( ! wp_next_scheduled( 'yourls_cron' ) ) {
wp_schedule_event( time(), 'hourly', 'yourls_cron' );
}
// Schedule the API ping test.
if ( ! wp_next_scheduled( 'yourls_test' ) ) {
wp_schedule_event( time(), 'twicedaily', 'yourls_test' );
}
}
/**
* Remove the cron jobs on deactivation.
*
* @return void
*/
public function remove_crons() {
// Fetch the timestamps/
$click = wp_next_scheduled( 'yourls_cron' );
$check = wp_next_scheduled( 'yourls_test' );
// Remove the jobs.
wp_unschedule_event( $click, 'yourls_cron', array() );
wp_unschedule_event( $check, 'yourls_test', array() );
}
} // End the class.
// Instantiate our class.
$YOURLSCreator = YOURLSCreator::getInstance();