| @@ -1,36 +1,90 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Hooks; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | use BitCode\BitForm\Admin\Admin_Bar; |
| 6 | 10 | use BitCode\BitForm\API\Route\Routes; |
| 7 | 11 | use BitCode\BitForm\Core\Ajax\AjaxService; |
| 12 | +use BitCode\BitForm\Core\Api\BitFormPublicApi; | |
| 8 | 13 | use BitCode\BitForm\Core\Capability\Request; |
| 9 | 14 | use BitCode\BitForm\Core\Database\FormModel; |
| 15 | +use BitCode\BitForm\Core\Fallback\FormFallback; | |
| 10 | 16 | use BitCode\BitForm\Core\Form\FormHandler; |
| 11 | 17 | use BitCode\BitForm\Core\Integration\Integrations; |
| 18 | +use BitCode\BitForm\Core\Util\CacheCompat; | |
| 12 | 19 | use BitCode\BitForm\Core\Util\FileDownloadProvider; |
| 13 | -use BitCode\BitForm\Core\Util\GutenBlockProvider; | |
| 20 | +use BitCode\BitForm\Core\Util\Translation\TranslationManager; | |
| 21 | +use BitCode\BitForm\Core\Util\Utilities; | |
| 22 | +use BitCode\BitForm\Core\WorkFlow\WorkflowExecutor; | |
| 23 | +use BitCode\BitForm\Frontend\ConversationalFormView; | |
| 24 | +use BitCode\BitForm\Frontend\StandaloneFormView; | |
| 25 | +use BitCode\BitForm\Widgets\RegisterBitformBricksWidget; | |
| 26 | +use BitCode\BitForm\Widgets\RegisterGutenBlock; | |
| 14 | 27 | |
| 15 | -class Hooks { | |
| 16 | - public static function init_hooks() { | |
| 28 | +class Hooks | |
| 29 | +{ | |
| 30 | + public static function init_hooks() | |
| 31 | + { | |
| 17 | 32 | add_action('init', [PostType::class, 'registerBitformsPostType']); |
| 18 | 33 | add_action('init', [PostType::class, 'registerCustomPostType']); |
| 34 | + add_action('update_option_bitform_custom_post_types', [PostType::class, 'scheduleRewriteFlush']); | |
| 35 | + add_filter('wp_robots', [PostType::class, 'noindexBitformsPages']); | |
| 19 | 36 | add_action('bitforms_exec_integrations', [Integrations::class, 'integrationExecutionHelper'], 1, 5); |
| 20 | - add_action('init', [Hooks::class, 'localization_setup']); | |
| 37 | + // Reclaim safety net for queued workflows whose browser-fired trigger was lost | |
| 38 | + add_action('bitforms_reclaim_workflow', [WorkflowExecutor::class, 'reclaim'], 10, 4); | |
| 39 | + add_action('bitforms_reclaim_sweep', [WorkflowExecutor::class, 'sweep']); | |
| 40 | + add_filter('bitform_default_submit_confirmation_excluded_action_ids', ['BitCode\BitForm\Core\WorkFlow\Helper', 'workflowReferencedActionIds'], 10, 3); | |
| 21 | 41 | add_action('init', [Hooks::class, 'init_classes']); |
| 22 | 42 | add_action('init', [Hooks::class, 'versionUpdateRunFallbacks']); |
| 23 | 43 | add_action('rest_api_init', [Hooks::class, 'registerRoutes']); |
| 24 | 44 | add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), [Hooks::class, 'plugin_action_links']); |
| 45 | + add_action('bitform_dequeue_scripts', [Hooks::class, 'dequeueScripts'], 1000, 100); | |
| 46 | + add_action('bitform_dequeue_styles', [Hooks::class, 'dequeueStyles'], 1000, 100); | |
| 47 | + add_action('init', [ConversationalFormView::class, 'conversationalFormView']); | |
| 48 | + add_action('init', [StandaloneFormView::class, 'standaloneFormView']); | |
| 49 | + add_action('bitform_admin_form_changed', [Hooks::class, 'invalidateCustomUrlCache']); | |
| 50 | + \BitCode\BitForm\Core\Util\CacheCompat::registerPurgeHooks(); | |
| 51 | + add_action('wp_footer', [Hooks::class, 'updateBitFormVersion'], 9999, 0); | |
| 52 | + | |
| 53 | + // Register the BitForm widget for Gutenberg, and Bricks Builder | |
| 54 | + add_action('init', [new RegisterGutenBlock(), 'register']); | |
| 55 | + add_action('init', [RegisterBitformBricksWidget::class, 'register_widgets'], 11); | |
| 56 | + // Add Bit Form menu to admin bar by "manage_bitform" capability | |
| 57 | + add_filter('bitforms_form_access_capability', [Hooks::class, 'bitformMenuAccessCapability']); | |
| 58 | + | |
| 59 | + // Public API bridge for sibling Bit Apps plugins (Bit CRM). Consumers call | |
| 60 | + add_filter('bitform/api/crm_integrated_forms', fn ($default = null) => BitFormPublicApi::getCrmIntegratedForms(), 10, 1); | |
| 61 | + add_filter('bitform/api/toggle_form_status', fn ($default = null, $formId = 0, $status = 0) => BitFormPublicApi::toggleFormStatus($formId, $status), 10, 3); | |
| 62 | + add_filter('bitform/api/create_form_url', fn ($default = null, $args = []) => BitFormPublicApi::getCreateFormUrl($args), 10, 2); | |
| 25 | 63 | } |
| 26 | 64 | |
| 27 | - public static function registerRoutes() { | |
| 65 | + public static function updateBitFormVersion() | |
| 66 | + { | |
| 67 | + $currentBitFormVersion = BITFORMS_VERSION; | |
| 68 | + $installedBitFormVersion = get_option('bitforms_version'); | |
| 69 | + if ($currentBitFormVersion !== $installedBitFormVersion) { | |
| 70 | + (new FormFallback())->resetJsGeneratedPageIds(); | |
| 71 | + // Without this the regenerated bundle keeps its ?bfv= URL and browsers | |
| 72 | + // and CDNs keep serving the stale file. | |
| 73 | + $formUpdateVersion = (int) get_option('bitform_form_update_version'); | |
| 74 | + update_option('bitform_form_update_version', $formUpdateVersion ? $formUpdateVersion + 1 : 1); | |
| 75 | + update_option('bitforms_version', $currentBitFormVersion); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + public static function registerRoutes() | |
| 80 | + { | |
| 28 | 81 | $routes = new Routes(); |
| 29 | 82 | $routes->register_routes(); |
| 30 | 83 | } |
| 31 | 84 | |
| 32 | - public static function isGeneratedScript($postId, $oldPost, $updatedPost) { | |
| 85 | + public static function isGeneratedScript($postId, $oldPost, $updatedPost) | |
| 86 | + { | |
| 33 | 87 | $oldContent = $oldPost->post_content; |
| 34 | 88 | $updatedContent = $updatedPost->post_content; |
| 35 | 89 | $oldShortCodeMatch = \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $oldContent, $oldShortCodes); |
| 36 | 90 | $updatedShortCodeMatch = \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $updatedContent, $updatedShortCodes); |
| @@ -48,10 +102,10 @@ | ||
| 48 | 102 | 'generated_script_page_ids' => ['operator' => 'LIKE', 'value' => $value], |
| 49 | 103 | ]; |
| 50 | 104 | $forms = $formModel->get('generated_script_page_ids,id', $condtions); |
| 51 | 105 | foreach ($forms as $form) { |
| 52 | - $pageIds = json_decode($form->generated_script_page_ids); | |
| 53 | - if (property_exists($pageIds, $postId)) { | |
| 106 | + $pageIds = Utilities::jsonObj($form->generated_script_page_ids ?? ''); | |
| 107 | + if ($pageIds && property_exists($pageIds, $postId)) { | |
| 54 | 108 | unset($pageIds->{$postId}); |
| 55 | 109 | $formModel->update(['generated_script_page_ids' => \wp_json_encode($pageIds)], ['id' => $form->id]); |
| 56 | 110 | } |
| 57 | 111 | } |
| @@ -58,35 +112,35 @@ | ||
| 58 | 112 | } |
| 59 | 113 | } |
| 60 | 114 | } |
| 61 | 115 | |
| 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() { | |
| 116 | + public static function init_classes() | |
| 117 | + { | |
| 67 | 118 | if (Request::Check('admin')) { |
| 68 | 119 | (new Admin_Bar())->register(); |
| 120 | + TranslationManager::boot('admin'); | |
| 69 | 121 | } |
| 70 | 122 | if (Request::Check('ajax')) { |
| 71 | 123 | new AjaxService(); |
| 124 | + TranslationManager::boot('ajax'); | |
| 72 | 125 | } |
| 73 | 126 | if (Request::Check('frontend')) { |
| 74 | 127 | $formHandler = new FormHandler(); |
| 75 | 128 | $formHandler->frontend; |
| 129 | + TranslationManager::boot('frontend'); | |
| 130 | + CacheCompat::register(); | |
| 76 | 131 | } |
| 77 | 132 | if (Request::isPluginPage()) { |
| 78 | 133 | (new FileDownloadProvider())->register(); |
| 79 | 134 | } |
| 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 | - } | |
| 135 | + // if (current_user_can('edit_posts')) { | |
| 136 | + // (new RegisterGutenBlock())->register(); | |
| 137 | + // } | |
| 138 | + include BITFORMS_PLUGIN_DIR_PATH . 'includes' . DIRECTORY_SEPARATOR . 'Frontend' . DIRECTORY_SEPARATOR . 'InitRoutes.php'; | |
| 86 | 139 | } |
| 87 | 140 | |
| 88 | - public static function versionUpdateRunFallbacks() { | |
| 141 | + public static function versionUpdateRunFallbacks() | |
| 142 | + { | |
| 89 | 143 | $dir = BITFORMS_PLUGIN_DIR_PATH . 'includes' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'Fallback'; |
| 90 | 144 | if (is_dir($dir) && is_file($dir . DIRECTORY_SEPARATOR . 'Init.php')) { |
| 91 | 145 | include $dir . DIRECTORY_SEPARATOR . 'Init.php'; |
| 92 | 146 | } |
| @@ -91,9 +145,58 @@ | ||
| 91 | 145 | include $dir . DIRECTORY_SEPARATOR . 'Init.php'; |
| 92 | 146 | } |
| 93 | 147 | } |
| 94 | 148 | |
| 95 | - public static function plugin_action_links($links) { | |
| 96 | - $links[] = '<a href="https://docs.form.bitapps.pro" target="_blank">' . __('Docs') . '</a>'; | |
| 149 | + public static function plugin_action_links($links) | |
| 150 | + { | |
| 151 | + $links[] = '<a href="https://bit-form.com/wp-docs/" target="_blank">' . __('Docs', 'bit-form') . '</a>'; | |
| 152 | + if (!Utilities::isProLicensed()) { | |
| 153 | + $links[] = '<a href="https://bit-form.com/#pricing" target="_blank"><strong>' . __('Upgrade to Pro', 'bit-form') . '</strong></a>'; | |
| 154 | + // $links[] = '<a href="https://bitapps.pro/christmas-wordpress-plugin-deal/#bit-form-pricing?link_type=promo&utm_source=bit-form&utm_medium=update_button&utm_campaign=christmas&utm_content=plugins_list_directory" target="_blank"><strong>' . __('Get 50% Off! Christmas Deal.', 'bit-form') . '</strong></a>'; | |
| 155 | + } | |
| 97 | 156 | return $links; |
| 157 | + } | |
| 158 | + | |
| 159 | + public static function dequeueScripts(...$formIds) | |
| 160 | + { | |
| 161 | + foreach ($formIds as $formId) { | |
| 162 | + wp_deregister_script("bitform-script-{$formId}"); | |
| 163 | + wp_dequeue_script("bitform-script-{$formId}"); | |
| 164 | + } | |
| 165 | + global $bitform_dequeued_scripts; | |
| 166 | + if (!is_array($bitform_dequeued_scripts)) { | |
| 167 | + $bitform_dequeued_scripts = []; | |
| 168 | + } | |
| 169 | + $bitform_dequeued_scripts = array_merge($bitform_dequeued_scripts, $formIds); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public static function dequeueStyles(...$formIds) | |
| 173 | + { | |
| 174 | + foreach ($formIds as $formId) { | |
| 175 | + wp_deregister_style("bitform-style-{$formId}"); | |
| 176 | + wp_dequeue_style("bitform-style-{$formId}"); | |
| 177 | + wp_deregister_style("bitform-style-{$formId}-formid"); | |
| 178 | + wp_dequeue_style("bitform-style-{$formId}-formid"); | |
| 179 | + } | |
| 180 | + global $bitform_dequeued_styles; | |
| 181 | + if (!is_array($bitform_dequeued_styles)) { | |
| 182 | + $bitform_dequeued_styles = []; | |
| 183 | + } | |
| 184 | + $bitform_dequeued_styles = array_merge($bitform_dequeued_styles, $formIds); | |
| 185 | + } | |
| 186 | + | |
| 187 | + public static function invalidateCustomUrlCache() | |
| 188 | + { | |
| 189 | + delete_transient('bitform_standalone_url_map'); | |
| 190 | + delete_transient('bitform_conversational_url_map'); | |
| 191 | + } | |
| 192 | + | |
| 193 | + public static function bitformMenuAccessCapability() | |
| 194 | + { | |
| 195 | + if (current_user_can('manage_bitform')) { | |
| 196 | + return 'manage_bitform'; | |
| 197 | + } elseif (current_user_can('access_bitform')) { | |
| 198 | + return 'access_bitform'; | |
| 199 | + } | |
| 200 | + return 'manage_options'; | |
| 98 | 201 | } |
| 99 | 202 | } |