| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Hooks; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Admin_Bar; |
| 6 |
use BitCode\BitForm\API\Route\Routes; |
| 7 |
use BitCode\BitForm\Core\Ajax\AjaxService; |
| 8 |
use BitCode\BitForm\Core\Capability\Request; |
| 9 |
use BitCode\BitForm\Core\Database\FormModel; |
| 10 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 11 |
use BitCode\BitForm\Core\Integration\Integrations; |
| 12 |
use BitCode\BitForm\Core\Util\FileDownloadProvider; |
| 13 |
use BitCode\BitForm\Core\Util\GutenBlockProvider; |
| 14 |
|
| 15 |
class Hooks { |
| 16 |
public static function init_hooks() { |
| 17 |
add_action('init', [PostType::class, 'registerBitformsPostType']); |
| 18 |
add_action('init', [PostType::class, 'registerCustomPostType']); |
| 19 |
add_action('bitforms_exec_integrations', [Integrations::class, 'integrationExecutionHelper'], 1, 5); |
| 20 |
add_action('init', [Hooks::class, 'localization_setup']); |
| 21 |
add_action('init', [Hooks::class, 'init_classes']); |
| 22 |
add_action('init', [Hooks::class, 'versionUpdateRunFallbacks']); |
| 23 |
add_action('rest_api_init', [Hooks::class, 'registerRoutes']); |
| 24 |
add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), [Hooks::class, 'plugin_action_links']); |
| 25 |
} |
| 26 |
|
| 27 |
public static function registerRoutes() { |
| 28 |
$routes = new Routes(); |
| 29 |
$routes->register_routes(); |
| 30 |
} |
| 31 |
|
| 32 |
public static function isGeneratedScript($postId, $oldPost, $updatedPost) { |
| 33 |
$oldContent = $oldPost->post_content; |
| 34 |
$updatedContent = $updatedPost->post_content; |
| 35 |
$oldShortCodeMatch = \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $oldContent, $oldShortCodes); |
| 36 |
$updatedShortCodeMatch = \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $updatedContent, $updatedShortCodes); |
| 37 |
|
| 38 |
if ($oldShortCodeMatch && $updatedShortCodeMatch) { |
| 39 |
$oldShortCodes = $oldShortCodes[2]; |
| 40 |
$updatedShortCodes = $updatedShortCodes[2]; |
| 41 |
|
| 42 |
$checkEqualArray = array_diff($oldShortCodes, $updatedShortCodes); |
| 43 |
|
| 44 |
if (!empty($checkEqualArray)) { |
| 45 |
$formModel = new FormModel(); |
| 46 |
$value = "%$postId%"; |
| 47 |
$condtions = [ |
| 48 |
'generated_script_page_ids' => ['operator' => 'LIKE', 'value' => $value], |
| 49 |
]; |
| 50 |
$forms = $formModel->get('generated_script_page_ids,id', $condtions); |
| 51 |
foreach ($forms as $form) { |
| 52 |
$pageIds = json_decode($form->generated_script_page_ids); |
| 53 |
if (property_exists($pageIds, $postId)) { |
| 54 |
unset($pageIds->{$postId}); |
| 55 |
$formModel->update(['generated_script_page_ids' => \wp_json_encode($pageIds)], ['id' => $form->id]); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static function localization_setup() { |
| 63 |
load_plugin_textdomain('bit-form', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); |
| 64 |
} |
| 65 |
|
| 66 |
public static function init_classes() { |
| 67 |
if (Request::Check('admin')) { |
| 68 |
(new Admin_Bar())->register(); |
| 69 |
} |
| 70 |
if (Request::Check('ajax')) { |
| 71 |
new AjaxService(); |
| 72 |
} |
| 73 |
if (Request::Check('frontend')) { |
| 74 |
$formHandler = new FormHandler(); |
| 75 |
$formHandler->frontend; |
| 76 |
} |
| 77 |
if (Request::isPluginPage()) { |
| 78 |
(new FileDownloadProvider())->register(); |
| 79 |
} |
| 80 |
if (current_user_can('edit_posts')) { |
| 81 |
(new GutenBlockProvider())->register(); |
| 82 |
} |
| 83 |
if (defined('BITAPPS_DEV') && BITAPPS_DEV) { |
| 84 |
include BITFORMS_PLUGIN_DIR_PATH . 'includes' . DIRECTORY_SEPARATOR . 'Frontend' . DIRECTORY_SEPARATOR . 'InitRoutes.php'; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public static function versionUpdateRunFallbacks() { |
| 89 |
$dir = BITFORMS_PLUGIN_DIR_PATH . 'includes' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'Fallback'; |
| 90 |
if (is_dir($dir) && is_file($dir . DIRECTORY_SEPARATOR . 'Init.php')) { |
| 91 |
include $dir . DIRECTORY_SEPARATOR . 'Init.php'; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public static function plugin_action_links($links) { |
| 96 |
$links[] = '<a href="https://docs.form.bitapps.pro" target="_blank">' . __('Docs') . '</a>'; |
| 97 |
return $links; |
| 98 |
} |
| 99 |
} |
| 100 |
|