-
Notifications
You must be signed in to change notification settings - Fork 0
/
YmWordpressPluginTemplate.php
206 lines (183 loc) · 6.08 KB
/
YmWordpressPluginTemplate.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
<?php
/**
*
* @package plugin-template
* @author Yanike Mann
* @copyright Yanike Mann
*
* @wordpress-plugin
* Plugin Name: Plugin Template
* Description: Plugin Template for WordPress
* Version: 1.0.0
* Requires at least: 5.2.0
* Requires PHP: 7.3
* Author: Yanike Mann
* Text Domain: plugin-template
*/
if (! defined('ABSPATH')) {
die();
}
if (! defined('PLUGIN_DIR')) {
define('PLUGIN_DIR', '/wp-content/plugins/YmWordpressPluginTemplate/');
}
if (! defined('PLUGINTEMPLATE_DIR')) {
define('PLUGINTEMPLATE_DIR', plugin_dir_path(__FILE__));
}
require_once PLUGINTEMPLATE_DIR . 'includes/Core.php';
class YmWordpressPluginTemplate
{
public function __construct()
{
add_action('init', array(
$this,
'action_init'
));
}
/**
* Activation of WordPress Plugin
*/
public function activate()
{
// generated a Custom Post Type (CPT)
$this->action_init();
// flush rewrite rules
flush_rewrite_rules(true);
}
/**
* Deactivation of WordPress Plugin
*/
public function deactivate()
{
// flush rewrite rules
flush_rewrite_rules(true);
}
/**
* Register custom post type
*/
public function action_init()
{
register_post_type('test_section', [
'label' => __('Test Section', 'txtdomain'),
'public' => true,
'show_in_menu' => true,
'menu_position' => 25,
'menu_icon' => 'dashicons-book',
'supports' => [
'title',
'thumbnail',
'author',
'revisions'
],
'show_in_rest' => true,
'taxonomies' => [
'test_section_item'
],
'labels' => [
'singular_name' => __('Test Section', 'txtdomain'),
'add_new_item' => __('Add new Test Section', 'txtdomain'),
'new_item' => __('New Test Section', 'txtdomain'),
'view_item' => __('View Test Section', 'txtdomain'),
'not_found' => __('No Test Sections found', 'txtdomain'),
'not_found_in_trash' => __('No Test Sections found in trash', 'txtdomain'),
'all_items' => __('All Test Sections', 'txtdomain'),
'insert_into_item' => __('Insert into test_section', 'txtdomain')
]
]);
register_taxonomy('test_section_item', [
'test_section'
], [
'label' => __('Item', 'txtdomain'),
'hierarchical' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'labels' => [
'singular_name' => __('Item', 'txtdomain'),
'all_items' => __('All Item', 'txtdomain'),
'edit_item' => __('Edit Item', 'txtdomain'),
'view_item' => __('View Item', 'txtdomain'),
'update_item' => __('Update Item', 'txtdomain'),
'add_new_item' => __('Add New Item', 'txtdomain'),
'new_item_name' => __('New Item Name', 'txtdomain'),
'search_items' => __('Search Items', 'txtdomain'),
'not_found' => __('No Items found', 'txtdomain')
]
]);
register_taxonomy_for_object_type('test_section_item', 'test_section');
}
}
// If class exists, create a new instance of the class
if (class_exists('YmWordpressPluginTemplate')) {
$ymWordpressPluginTemplate = new YmWordpressPluginTemplate();
}
function plugin_activation()
{
register_uninstall_hook(__FILE__, 'plugintemplate_uninstallation');
global $table_prefix, $wpdb;
/*
$tblname = 'newtable';
$wp_newtable_table = $table_prefix . $tblname;
// Check to see if the table exists already, if not, then create it
if($wpdb->get_var( "show tables like '$wp_newtable_table'" ) != $wp_newtable_table)
{
$sql = "CREATE TABLE `". $wp_newtable_table . "` ( ";
$sql .= " `id` int(11) NOT NULL auto_increment, ";
$sql .= " `client_id` longtext NOT NULL, ";
$sql .= " `client_secret` longtext NOT NULL, ";
$sql .= " PRIMARY KEY `order_id` (`id`) ";
$sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; ";
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
$wpdb->insert($wp_newtable_table,
array(
'client_id' => 'XXX',
'client_secret' => 'XXX',
)
);
*/
}
/**
* Uninstallation of WordPress Plugin
*/
function plugintemplate_uninstallation()
{
// Access the database via SQL
global $wpdb;
// Delete all posts of articles
//$wpdb->query("DELETE FROM wp_posts WHERE post_type = 'test_section'");
// Delete all post_id data not found in wp_posts id
// $wpdb->query("DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID FROM wp_posts)");
// Delete all object_id data not found in wp_posts id
// $wpdb->query("DELETE FROM wp_term_relationships WHERE object_id NOT IN(SELECT ID FROM wp_posts)");
}
// activation
register_activation_hook(__FILE__, 'plugin_activation');
// activation
register_deactivation_hook(__FILE__, 'plugin_deactivation');
function add_plugintemplate_stylesheet()
{
wp_register_style('plugintemplate_style', PLUGIN_DIR . 'assets/styles/style.css');
wp_enqueue_style('plugintemplate_style');
}
function add_plugintemplate_scripts()
{
if (! is_admin()) {
wp_register_script('plugintemplate_script', PLUGIN_DIR . 'assets/scripts/script.js');
wp_enqueue_script('plugintemplate_script');
}
}
function testSec($atts)
{
$core = new \Core();
$core->testSection($atts, PLUGINTEMPLATE_DIR);
}
function add_plugintemplate_shortcodes()
{
add_shortcode("plugintemplate_testsection", "testSec");
}
add_action('wp_print_styles', 'add_plugintemplate_stylesheet');
add_action('wp_print_scripts', 'add_plugintemplate_scripts');
add_action('init', 'add_plugintemplate_shortcodes');
// Add AJAX Calls to WP
$core = new Core();
$core->ajaxCalls();