| 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("bitforms_exec_integrations", array(Integrations::class, 'integrationExecutionHelper'), 1, 5); |
| 60 |
// Localize our plugin |
| 61 |
add_action('init', array($this, 'localization_setup')); |
| 62 |
|
| 63 |
// initialize the classes |
| 64 |
add_action('init', array($this, 'init_classes')); |
| 65 |
|
| 66 |
add_action('init', array($this, 'entryMetaKeyUpdate')); |
| 67 |
|
| 68 |
add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), array($this, 'plugin_action_links')); |
| 69 |
add_action('rest_api_init', [$this, 'register_api']); |
| 70 |
} |
| 71 |
|
| 72 |
public function register_api() |
| 73 |
{ |
| 74 |
$routes = new Routes(); |
| 75 |
$routes->register_routes(); |
| 76 |
} |
| 77 |
|
| 78 |
public function entryMetaKeyUpdate() |
| 79 |
{ |
| 80 |
$installed = get_option('bitforms_installed'); |
| 81 |
$oldversion = null; |
| 82 |
if ($installed) { |
| 83 |
$oldversion = get_option('bitforms_version'); |
| 84 |
} |
| 85 |
if ($oldversion && version_compare($oldversion, BITFORMS_VERSION, '!=')) { |
| 86 |
update_option('bitforms_version', BITFORMS_VERSION); |
| 87 |
if (version_compare('1.0.4', $oldversion, '>=')) { |
| 88 |
// if version is lower or equal than 1.0.4 |
| 89 |
$formEntryMetaChange = new FormEntryMetaChange(); |
| 90 |
$formEntryMetaChange->changeMeta(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Updates bit form content tables on wp db |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function update_tables() |
| 101 |
{ |
| 102 |
if (!current_user_can('manage_options')) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
global $bitforms_db_version; |
| 107 |
$installed_db_version = get_site_option("bitforms_db_version"); |
| 108 |
if ($installed_db_version != $bitforms_db_version) { |
| 109 |
DB::migrate(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Register post type for bitforms |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function registerBitformsPostType() |
| 119 |
{ |
| 120 |
$args = array( |
| 121 |
'label' => __('Bitform Pages', 'bitform'), |
| 122 |
'public' => true, |
| 123 |
'show_ui' => true, |
| 124 |
'show_in_menu' => false, |
| 125 |
'capability_type' => 'page', |
| 126 |
'hierarchical' => false, |
| 127 |
'query_var' => false, |
| 128 |
'supports' => array('title'), |
| 129 |
'show_in_rest' => false |
| 130 |
); |
| 131 |
register_post_type('bitforms', $args); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Initialize plugin for localization |
| 136 |
* |
| 137 |
* @uses load_plugin_textdomain() |
| 138 |
*/ |
| 139 |
public function localization_setup() |
| 140 |
{ |
| 141 |
load_plugin_textdomain('bitform', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Instantiate the required classes |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function init_classes() |
| 150 |
{ |
| 151 |
if (Request::Check('admin')) { |
| 152 |
(new Admin_Bar())->register(); |
| 153 |
} |
| 154 |
if (Request::Check('ajax')) { |
| 155 |
new AjaxService(); |
| 156 |
} |
| 157 |
if (Request::Check('frontend')) { |
| 158 |
$formHandler = new FormHandler(); |
| 159 |
$formHandler->frontend; |
| 160 |
} |
| 161 |
if (Request::isPluginPage()) { |
| 162 |
(new FileDownloadProvider())->register(); |
| 163 |
} |
| 164 |
if (current_user_can('edit_posts')) { |
| 165 |
(new GutenBlockProvider())->register(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Plugin action links |
| 171 |
* |
| 172 |
* @param array $links |
| 173 |
* |
| 174 |
* @return array |
| 175 |
*/ |
| 176 |
public function plugin_action_links($links) |
| 177 |
{ |
| 178 |
$links[] = '<a href="https://www.bitpress.pro" target="_blank">' . __('Docs', 'bitform') . '</a>'; |
| 179 |
|
| 180 |
return $links; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
public function init_plugin() |
| 185 |
{ |
| 186 |
$this->init_hooks(); |
| 187 |
$this->update_tables(); |
| 188 |
do_action('bitform_loaded'); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Retrieves the main instance of the plugin. |
| 193 |
* |
| 194 |
* @since 1.0.0-alpha |
| 195 |
* |
| 196 |
* @return BITFORM Plugin main instance. |
| 197 |
*/ |
| 198 |
public static function instance() |
| 199 |
{ |
| 200 |
return static::$instance; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Loads the plugin main instance and initializes it. |
| 205 |
* |
| 206 |
* @param string $main_file Absolute path to the plugin main file. |
| 207 |
* |
| 208 |
* @return bool True if the plugin main instance could be loaded, false otherwise. |
| 209 |
*/ |
| 210 |
public static function load($main_file) |
| 211 |
{ |
| 212 |
if (null !== static::$instance) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
static::$instance = new static($main_file); |
| 217 |
static::$instance->register(); |
| 218 |
return true; |
| 219 |
} |
| 220 |
} |
| 221 |
|