| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm; |
| 4 |
|
| 5 |
/** |
| 6 |
* Main class for the plugin. |
| 7 |
* |
| 8 |
* @since 1.0.0-alpha |
| 9 |
*/ |
| 10 |
|
| 11 |
use BitCode\BitForm\Admin\Admin_Bar; |
| 12 |
use BitCode\BitForm\Core\Database\DB; |
| 13 |
use BitCode\BitForm\Core\Util\Activation; |
| 14 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 15 |
use BitCode\BitForm\Core\Ajax\AjaxService; |
| 16 |
use BitCode\BitForm\Core\Util\Deactivation; |
| 17 |
use BitCode\BitForm\Core\Util\Uninstallation; |
| 18 |
use BitCode\BitForm\Core\Capability\Request; |
| 19 |
use BitCode\BitForm\Core\Util\GutenBlockProvider; |
| 20 |
use BitCode\BitForm\Core\Integration\Integrations; |
| 21 |
use BitCode\BitForm\Core\Util\FileDownloadProvider; |
| 22 |
use BitCode\BitForm\Core\Fallback\FormEntryMetaChange; |
| 23 |
use BitCode\BitForm\API\Route\Routes; |
| 24 |
|
| 25 |
final class Plugin |
| 26 |
{ |
| 27 |
|
| 28 |
/** |
| 29 |
* Main instance of the plugin. |
| 30 |
* |
| 31 |
* @since 1.0.0-alpha |
| 32 |
* @var Plugin|null |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Registers the plugin with WordPress. |
| 39 |
* |
| 40 |
* @since 1.0.0-alpha |
| 41 |
*/ |
| 42 |
public function register() |
| 43 |
{ |
| 44 |
add_action('plugins_loaded', array($this, 'init_plugin')); |
| 45 |
(new Activation())->activate(); |
| 46 |
(new Deactivation())->register(); |
| 47 |
(new Uninstallation())->register(); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Initialize the hooks |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function init_hooks() |
| 57 |
{ |
| 58 |
add_action('init', array($this, 'registerBitformsPostType')); |
| 59 |
add_action('init', array($this, 'registerCustomPostType')); |
| 60 |
add_action("bitforms_exec_integrations", array(Integrations::class, 'integrationExecutionHelper'), 1, 5); |
| 61 |
// Localize our plugin |
| 62 |
add_action('init', array($this, 'localization_setup')); |
| 63 |
|
| 64 |
// initialize the classes |
| 65 |
add_action('init', array($this, 'init_classes')); |
| 66 |
|
| 67 |
add_action('init', array($this, 'entryMetaKeyUpdate')); |
| 68 |
|
| 69 |
add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), array($this, 'plugin_action_links')); |
| 70 |
add_action('rest_api_init', [$this, 'register_api']); |
| 71 |
} |
| 72 |
|
| 73 |
public function register_api() |
| 74 |
{ |
| 75 |
$routes = new Routes(); |
| 76 |
$routes->register_routes(); |
| 77 |
} |
| 78 |
|
| 79 |
public function entryMetaKeyUpdate() |
| 80 |
{ |
| 81 |
$installed = get_option('bitforms_installed'); |
| 82 |
$oldversion = null; |
| 83 |
if ($installed) { |
| 84 |
$oldversion = get_option('bitforms_version'); |
| 85 |
} |
| 86 |
if ($oldversion && version_compare($oldversion, BITFORMS_VERSION, '!=')) { |
| 87 |
update_option('bitforms_version', BITFORMS_VERSION); |
| 88 |
if (version_compare('1.0.4', $oldversion, '>=')) { |
| 89 |
// if version is lower or equal than 1.0.4 |
| 90 |
$formEntryMetaChange = new FormEntryMetaChange(); |
| 91 |
$formEntryMetaChange->changeMeta(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Updates bit form content tables on wp db |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function update_tables() |
| 102 |
{ |
| 103 |
if (!current_user_can('manage_options')) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
global $bitforms_db_version; |
| 108 |
$installed_db_version = get_site_option("bitforms_db_version"); |
| 109 |
if ($installed_db_version != $bitforms_db_version) { |
| 110 |
DB::migrate(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register post type for bitforms |
| 116 |
* |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
public function registerBitformsPostType() |
| 120 |
{ |
| 121 |
$args = array( |
| 122 |
'label' => __('Bitform Pages', 'bitform'), |
| 123 |
'public' => true, |
| 124 |
'show_ui' => true, |
| 125 |
'show_in_menu' => false, |
| 126 |
'capability_type' => 'page', |
| 127 |
'hierarchical' => false, |
| 128 |
'query_var' => false, |
| 129 |
'supports' => array('title'), |
| 130 |
'show_in_rest' => false |
| 131 |
); |
| 132 |
register_post_type('bitforms', $args); |
| 133 |
} |
| 134 |
|
| 135 |
public function registerCustomPostType() |
| 136 |
{ |
| 137 |
flush_rewrite_rules( false ); |
| 138 |
$cpts = get_option('bitform_custom_post_types'); |
| 139 |
if (!empty($cpts)) { |
| 140 |
foreach ($cpts as $cpt ) { |
| 141 |
$labels = array( |
| 142 |
'name' => _x($cpt->name, 'Post type general name', 'textdomain'), |
| 143 |
'singular_name' => _x($cpt->singular_label, 'Post type singular name', 'textdomain'), |
| 144 |
'menu_name' => _x($cpt->menu_name, 'Admin Menu text', 'textdomain'), |
| 145 |
); |
| 146 |
$args = array( |
| 147 |
'labels' => $labels, |
| 148 |
'public' => $cpt->public == 1 ? true : false, |
| 149 |
'publicly_queryable' => $cpt->public_queryable == 1 ? true : false, |
| 150 |
'show_ui' => $cpt->show_ui == 1 ? true : false, |
| 151 |
'show_in_menu' => $cpt->show_in_menu == 1 ? true : false, |
| 152 |
'query_var' => true, |
| 153 |
'rewrite' => array('slug' => $cpt->name), |
| 154 |
'capability_type' => 'post', |
| 155 |
'has_archive' => true, |
| 156 |
'hierarchical' => false, |
| 157 |
'show_in_rest' => isset($cpt->show_in_rest) == 1 ? true : false, |
| 158 |
'menu_icon' => $cpt->menu_icon, |
| 159 |
'supports' => array('title','editor', 'author', 'excerpt', 'comments'), |
| 160 |
); |
| 161 |
register_post_type($cpt->name, $args); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Initialize plugin for localization |
| 168 |
* |
| 169 |
* @uses load_plugin_textdomain() |
| 170 |
*/ |
| 171 |
public function localization_setup() |
| 172 |
{ |
| 173 |
load_plugin_textdomain('bitform', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Instantiate the required classes |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function init_classes() |
| 182 |
{ |
| 183 |
if (Request::Check('admin')) { |
| 184 |
(new Admin_Bar())->register(); |
| 185 |
} |
| 186 |
if (Request::Check('ajax')) { |
| 187 |
new AjaxService(); |
| 188 |
} |
| 189 |
if (Request::Check('frontend')) { |
| 190 |
$formHandler = new FormHandler(); |
| 191 |
$formHandler->frontend; |
| 192 |
} |
| 193 |
if (Request::isPluginPage()) { |
| 194 |
(new FileDownloadProvider())->register(); |
| 195 |
} |
| 196 |
if (current_user_can('edit_posts')) { |
| 197 |
(new GutenBlockProvider())->register(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Plugin action links |
| 203 |
* |
| 204 |
* @param array $links |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public function plugin_action_links($links) |
| 209 |
{ |
| 210 |
$links[] = '<a href="https://www.bitpress.pro" target="_blank">' . __('Docs', 'bitform') . '</a>'; |
| 211 |
|
| 212 |
return $links; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
public function init_plugin() |
| 217 |
{ |
| 218 |
$this->init_hooks(); |
| 219 |
$this->update_tables(); |
| 220 |
do_action('bitform_loaded'); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Retrieves the main instance of the plugin. |
| 225 |
* |
| 226 |
* @since 1.0.0-alpha |
| 227 |
* |
| 228 |
* @return BITFORM Plugin main instance. |
| 229 |
*/ |
| 230 |
public static function instance() |
| 231 |
{ |
| 232 |
return static::$instance; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Loads the plugin main instance and initializes it. |
| 237 |
* |
| 238 |
* @param string $main_file Absolute path to the plugin main file. |
| 239 |
* |
| 240 |
* @return bool True if the plugin main instance could be loaded, false otherwise. |
| 241 |
*/ |
| 242 |
public static function load($main_file) |
| 243 |
{ |
| 244 |
if (null !== static::$instance) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
static::$instance = new static($main_file); |
| 249 |
static::$instance->register(); |
| 250 |
return true; |
| 251 |
} |
| 252 |
} |
| 253 |
|