-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-based-plugin.php
More file actions
182 lines (162 loc) · 4.73 KB
/
Copy pathapi-based-plugin.php
File metadata and controls
182 lines (162 loc) · 4.73 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
<?php
/**
* Plugin Name: API Based Plugin - Piyush Asthana
* Plugin URI: https://awesomemotive.com/
* Description: Custom Plugin for accessing the data from API endpoint using Ajax.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 8.0
* Author: Piyush Asthana
* Author URI: https://awesomemotive.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: api-based-plugin
* Domain Path: /languages
*
* @package Api_Based_Plugin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'API_Based_Plugin' ) ) :
/**
* Main API_Based_Plugin Class.
*
* @class API_Based_Plugin
* @version 1.0.0
*/
class API_Based_Plugin {
/**
* The single instance of the class.
*
* @var API_Based_Plugin
* @since 1.0.0
*/
protected static $instance = null;
/**
* Main API_Based_Plugin Instance.
*
* Ensures only one instance of API_Based_Plugin is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see instantiateb_API_Based_Plugin()
* @return API_Based_Plugin - Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* API_Based_Plugin Constructor.
*
* @since 1.0.0
*/
public function __construct() {
$this->define_constants();
$this->init_hooks();
$this->set_locale();
}
/**
* Hook into actions and filters.
*
* @since 1.0.0
*/
private function init_hooks() {
$this->includes();
register_activation_hook( __FILE__, array( $this, 'api_based_plugin_activate' ) );
}
/**
* Define API_Based_Plugin Constants.
*
* @since 1.0.0
*/
private function define_constants() {
$this->define( 'API_BASED_PLUGIN_PLUGIN_FILE', __FILE__ );
$this->define( 'API_BASED_PLUGIN_PLUGIN_VERSION', '1.0.0' );
$this->define( 'API_BASED_PLUGIN_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
$this->define( 'API_BASED_PLUGIN_VERSION', API_BASED_PLUGIN_PLUGIN_VERSION );
$this->define( 'API_BASED_PLUGIN_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
$this->define( 'API_BASED_PLUGIN_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Define constant if not already set.
*
* @param string $name Define constant name.
* @param string|bool $value Define constant value.
* @since 1.0.0
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Api_Based_Plugin_i18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 1.0.0
* @access private
*/
private function set_locale() {
load_plugin_textdomain( 'api-based-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Include required core files used in admin and on the frontend.
*
* @since 1.0.0
*/
public function includes() {
/* Enqueue admin page file. */
require_once 'includes/admin/class-api-based-plugin-admin.php';
/* Enqueue admin page file. */
require_once 'includes/admin/class-api-based-admin-table.php';
/* Enqueue front-end page file. */
require_once 'includes/front-end/class-api-based-plugin-front-end.php';
/* Enqueue front-end page file. */
require_once 'includes/class-api-based-wp-cli-refresh-data.php';
/* Enqueue Gutenberg Block. */
require_once 'block/api-based-plugin-block.php';
}
/**
* Load default admin settings on plugin activation.
*
* @since 1.0.0
*/
public function api_based_plugin_activate() {
global $wpdb;
$update_version = '1.0.0';
$installed_ver = get_option( 'plugin_db_version' );
if ( $installed_ver !== $update_version ) {
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'api_data';
$sql = "CREATE TABLE `$table_name` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`data_id` int(11) NOT NULL,
`first_name` varchar(220) DEFAULT NULL,
`last_name` varchar(220) DEFAULT NULL,
`email` varchar(220) DEFAULT NULL,
`date` varchar(220) DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
update_option( 'plugin_db_version', $update_version );
}
}
}
endif;
/**
* Main instance of API_Based_Plugin.
*
* Returns the main instance of API_Based_Plugin to prevent the need to use globals.
*
* @since 1.0.0
* @return API_Based_Plugin
*/
API_Based_Plugin::instance();