-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-fragmention.php
executable file
·142 lines (121 loc) · 4.58 KB
/
wp-fragmention.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
<?php
/**
* Plugin Name: WP Fragmention
* Plugin URI: https://christiaanconover.com/code/wp-fragmention?ref=plugin-data
* Description: Add support for Fragmention links to WordPress.
* Version: 0.1.4
* Author: Christiaan Conover
* Author URI: https://christiaanconover.com?ref=wp-fragmention-plugin-author-uri
* License: GPLv2
* @package cconover
* @subpackage fragmention
**/
namespace cconover;
/**
* Main plugin class
*/
class Fragmention {
// Plugin constants
const ID = 'cc-fragmention'; // Plugin ID
const NAME = 'Fragmention '; // Plugin name
const VERSION = '0.1.4'; // Plugin version
const WPVER = '2.7'; // Minimum version of WordPress required for this plugin
const PREFIX = 'cc_fragmention_'; // Plugin database/method prefix
// Class properties
private $options; // Plugin options and settings
// Class constructor
function __construct() {
// Get plugin options from database
$this->options = get_option( self::PREFIX . 'options' );
// Add the script
add_action( 'wp_enqueue_scripts', array( &$this, 'loadscript' ) );
// Admin
if ( is_admin() ) {
// Admin initialization
$this->admin_initialize();
// Activation and deactivation hooks
register_activation_hook( __FILE__, array( &$this, 'activate' ) ); // Plugin activation
register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) ); // Plugin deactivation
/* End hooks and filters */
}
} // End __construct()
/* ===== Add Script ===== */
// Load the Fragmention script
function loadscript() {
// Use the WordPress hook to add the script inside <head>
wp_enqueue_script(
self::ID, // Handle for the script
plugins_url( 'assets/js/fragmention.min.js', __FILE__ ), // Path to the script file
array(), // Script dependencies
self::VERSION // Script version
);
// Add stylesheet for Fragmention highlighting
wp_enqueue_style(
self::ID, // Handle for the stylesheet
plugins_url( 'assets/css/fragmention.css', __FILE__ ), // Path to the stylesheet file
array(), // Stylesheet dependencies
self::VERSION // Stylesheet version
);
// If tooltip-on-highlight is enabled, add the script for it
if ( ! empty( $this->options['tooltip'] ) ) {
wp_enqueue_script(
self::ID . '-highlight', // Handle for the script
plugins_url( 'assets/js/highlighter.js', __FILE__ ), // Path to the script file
array( 'jquery' ), // Script dependencies
self::VERSION // Script version
);
}
} // End loadscript()
/* ===== Admin Initialization ===== */
function admin_initialize() {
// Run upgrade process
$this->upgrade();
} // End admin_initialize()
// Plugin upgrade
function upgrade() {
// Check whether the database-stored plugin version number is less than the current plugin version number, or whether there is no plugin version saved in the database
if ( ! empty( $this->options['dbversion'] ) && version_compare( $this->options['dbversion'], self::VERSION, '<' ) ) {
// Set local variable for options (always the first step in the upgrade process)
$options = $this->options;
// If the 'tooltip' option isn't present, add it and set it to active
if ( empty( $options['tooltip'] ) ) {
$options['tooltip'] = 'yes';
}
/* Update the plugin options saved in the database (always the last step of the upgrade process) */
// Set the value of the plugin version
$options['dbversion'] = self::VERSION;
// Save to the database
update_option( self::PREFIX . 'options', $options );
/* End update plugin version */
}
} // End upgrade()
/*
===== End Admin Initialization =====
*/
/*
===== Plugin Activation and Deactivation =====
*/
// Plugin activation
public function activate() {
// Check to make sure the version of WordPress being used is compatible with the plugin
if ( version_compare( get_bloginfo( 'version' ), self::WPVER, '<' ) ) {
wp_die( 'Your version of WordPress is too old to use this plugin. Please upgrade to the latest version of WordPress.' );
}
// Default plugin options
$options = array(
'tooltip' => 'yes', // Display tooltip containing fragmention link when text is highlighted
'dbversion' => self::VERSION, // Current plugin version
);
// Add options to database
add_option( self::PREFIX . 'options', $options );
} // End activate()
// Plugin deactivation
public function deactivate() {
// Remove the plugin options from the database
delete_option( self::PREFIX . 'options' );
} // End deactivate
/* ===== End Plugin Activation and Deactivation ===== */
} // End main plugin class
// Create plugin object
$cc_fragmention = new \cconover\Fragmention;
?>