-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadphone_ranker.php
266 lines (223 loc) · 9.96 KB
/
headphone_ranker.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* @package headphone_ranker
*/
/*
Plugin Name: Headphone Ranker
Plugin URI: https://github.com/axawebs/headphoneranker
Description: Custom created plugin for Headphone Ranking on Wordpress
Version: 1.0
Author: Chandima Jayasiri
Author URI: mailto:[email protected]
License: GPLV2 or later
Text Domain: headphone_ranker
*/
/*
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 (at your option) 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
if (! defined( 'ABSPATH') ){
die;
}
class headphoneRanker
{
public $plugin_name;
public $settings = array();
public $sections = array();
public $fields = array();
//Method Access Modifiers
// public - can be accessed from outside the class
// protected - can only be accessed within the class ($this->protected_method())
// protected - can only be accessed from constructor
function __construct(){
//add_action ('init', array($this, 'custom_post_type')); // tell wp to execute method on init
$this->plugin_name = plugin_basename( __FILE__ );
}
function register(){
add_shortcode( 'headphone_ranker', array($this, 'hr_shortcode_frontend') );
add_action( 'admin_enqueue_scripts', array($this, 'enqueue_admin') );
add_action( 'wp_enqueue_scripts', array($this, 'enqueue') );
add_action ( 'admin_menu', array( $this, 'add_admin_pages' ));
add_filter ("plugin_action_links_$this->plugin_name", array ($this, 'settings_link'));
// add_filter( 'single_template', array($this, 'load_custom_post_specific_template'));
}
function add_admin_pages(){
add_menu_page( 'Headphone Ranker Settings', 'HRanker Settings', 'manage_options', 'headphone_ranker_settings', array($this,'admin_index'), 'dashicons-list-view', 100);
}
function admin_index(){
//require template
require_once plugin_dir_path( __FILE__ ) . 'templates/admin.php';
}
function settings_link($links){
//add custom setting link
$settings_link = '<a href="admin.php?page=headphone_ranker_settings">Settings Page</a>';
array_push ( $links, $settings_link );
return $links;
}
function activate(){
// Plugin activated state
// generate a Custom Post Style
// $this->custom_post_type();
$this->create_table();
// Flush rewrite rules
flush_rewrite_rules();
}
function deactivate(){
// Plugin deactivate state
//Flush rewrite rules
flush_rewrite_rules();
}
function uninstall(){
//Plugin deleted
//delete Custom Post Style
//delete all plugin data from the DB
}
function create_table(){
// create table if not exist
global $wpdb;
// Headphones
$table_name = $wpdb->prefix."hranker_headphones";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL AUTO_INCREMENT,
rank VARCHAR(1),
brand VARCHAR(50),
device VARCHAR(50),
price VARCHAR(20),
value INT(2),
principle VARCHAR(100),
overall_timbre VARCHAR(300),
summary TEXT,
ganre_focus VARCHAR(300),
PRIMARY KEY (id))';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("headphoneranker_db_version", "1.0");
}
// IEM
$table_name = $wpdb->prefix."hranker_iem";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL AUTO_INCREMENT,
rank VARCHAR(1),
brand VARCHAR(50),
device VARCHAR(50),
price VARCHAR(20),
value INT(2),
overall_timbre VARCHAR(300),
summary TEXT,
ganre_focus VARCHAR(300),
PRIMARY KEY (id))';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("headphoneranker_db_version", "1.1");
}
// Earbuds
$table_name = $wpdb->prefix."hranker_earbuds";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL AUTO_INCREMENT,
rank VARCHAR(1),
brand VARCHAR(50),
device VARCHAR(50),
price VARCHAR(20),
value INT(2),
overall_timbre VARCHAR(300),
summary TEXT,
ganre_focus VARCHAR(300),
PRIMARY KEY (id))';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("headphoneranker_db_version", "1.2");
}
// settings
$table_name = $wpdb->prefix."hranker_settings";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL AUTO_INCREMENT,
headphones_html TEXT,
iem_html TEXT,
earbuds_html TEXT,
banner_image1 TEXT,
banner_image2 TEXT,
banner_image3 TEXT,
banner_image4 TEXT,
banner_url1 TEXT,
banner_url2 TEXT,
banner_url3 TEXT,
banner_url4 TEXT,
PRIMARY KEY (id))';
$sql_insert = "INSERT INTO $table_name
VALUES (1,'- Headphones HTML goes here -','- IEM HTML goes here -','- EarBuds HTML goes here -','small_banner_default.jpg','small_banner_default.jpg','small_banner_default.jpg','small_banner_default.jpg','')";
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
dbDelta($sql_insert);
add_option("headphoneranker_db_version", "1.3");
}
}
//Enqueue on admin pages
function enqueue_admin($hook_suffix){
if (strpos($hook_suffix, 'headphone_ranker_settings') !== false) {
//Bootstrap
wp_enqueue_style( 'bootstrap4_styles', plugins_url('/assets/bootstrap4/bootstrap_4_5_2_min.css',__FILE__));
wp_enqueue_script( 'bootstrap4_scripts', plugins_url('/assets/bootstrap4/bootstrap_4_5_2_min.js',__FILE__), array('jquery','headphoneranker_popper_scripts'));
//Font-Awesome
wp_enqueue_style( 'fontawesome_css', plugins_url('/assets/font_awesome/css/font-awesome.css',__FILE__));
//Admin scripts and styles
wp_enqueue_style( 'headphoneranker_admin_styles', plugins_url('/assets/hranker_admin_style.css',__FILE__));
wp_enqueue_script( 'headphoneranker_admin_script', plugins_url('/assets/hranker_admin_scripts.js',__FILE__), array('jquery'));
//Select2
wp_enqueue_style( 'headphoneranker_select2_styles', plugins_url('/assets/select2/select2.css',__FILE__));
wp_enqueue_script( 'headphoneranker_select2_scripts', plugins_url('/assets/select2/select2.full.js',__FILE__), array('jquery'));
//Popper
wp_enqueue_script( 'headphoneranker_popper_scripts', plugins_url('/assets/popper/popper.min.js',__FILE__), array('jquery'));
//Table2Csv
wp_enqueue_script( 'headphoneranker_table2csv_scripts', plugins_url('/assets/table2csv/table2csv.js',__FILE__), array('jquery'));
}
}
//Enqueue on all other pages
function enqueue(){
//Load only on page id = 2
global $post;
$post_slug = $post->post_name;
//echo($post_slug);
if ( $post_slug=="headphone-ranking" || $post_slug=="iem-ranking" || $post_slug=="earbuds-ranking" ){
//Bootstrap
wp_enqueue_style( 'bootstrap4_styles', plugins_url('/assets/bootstrap4/bootstrap_4_5_2_min.css',__FILE__),93);
wp_enqueue_script( 'bootstrap4_scripts', plugins_url('/assets/bootstrap4/bootstrap_4_5_2_min.js',__FILE__), array('jquery','headphoneranker_popper_scripts'));
//Font-Awesome
wp_enqueue_style( 'fontawesome_css', plugins_url('/assets/font_awesome/css/font-awesome.css',__FILE__),90);
//FrontEnd scripts and styles
wp_enqueue_style( 'headphoneranker_styles', plugins_url('/assets/hranker_style.css',__FILE__),99);
wp_enqueue_script( 'headphoneranker_script', plugins_url('/assets/hranker_scripts.js',__FILE__), array('jquery'));
//Select2
wp_enqueue_style( 'headphoneranker_select2_styles', plugins_url('/assets/select2/select2.css',__FILE__),98);
wp_enqueue_script( 'headphoneranker_select2_scripts', plugins_url('/assets/select2/select2.full.js',__FILE__), array('jquery'));
//Popper
wp_enqueue_script( 'headphoneranker_popper_scripts', plugins_url('/assets/popper/popper.min.js',__FILE__), array('jquery'));
//Sharer
wp_enqueue_script( 'headphoneranker_sharer_scripts', plugins_url('/assets/sharer/sharer.min.js',__FILE__), array('jquery'));
}
}
//------ Shortcode
function hr_shortcode_frontend($atts){
include 'templates/hr_shortcode.php';
}
}
if ( class_exists('headphoneRanker') ){
$headphone_ranker = new headphoneRanker();
$headphone_ranker -> register();
}
//activate
register_activation_hook (__FILE__, array($headphone_ranker, 'activate'));
//deactivation
register_deactivation_hook (__FILE__, array($headphone_ranker, 'deactivate'));