-
Notifications
You must be signed in to change notification settings - Fork 1
weDocs integration added #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e0978cb
a67215e
994fac3
b859842
4d88895
abafa48
ee488fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| namespace BitApps\Integrations\Actions\WeDocs; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| use BitApps\Integrations\Config; | ||||||||||||||||||||||||||||||
| use BitApps\Integrations\Core\Util\Common; | ||||||||||||||||||||||||||||||
| use BitApps\Integrations\Core\Util\Hooks; | ||||||||||||||||||||||||||||||
| use BitApps\Integrations\Log\LogHandler; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Provide functionality for weDocs record operations. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| class RecordApiHelper | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| private $_integrationID; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private $_integrationDetails; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public function __construct($integrationDetails, $integId) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $this->_integrationDetails = $integrationDetails; | ||||||||||||||||||||||||||||||
| $this->_integrationID = $integId; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public function execute($fieldValues, $fieldMap) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| if (!class_exists('WeDocs')) { | ||||||||||||||||||||||||||||||
| $response = [ | ||||||||||||||||||||||||||||||
| 'success' => false, | ||||||||||||||||||||||||||||||
| 'message' => __('weDocs is not installed or activated', 'bit-integrations'), | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| LogHandler::save($this->_integrationID, ['type' => 'weDocs', 'type_name' => 'check'], 'error', $response); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return $response; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $mainAction = $this->_integrationDetails->mainAction ?? ''; | ||||||||||||||||||||||||||||||
| $fieldData = $this->generateReqDataFromFieldMap($fieldMap, $fieldValues); | ||||||||||||||||||||||||||||||
| $payload = $this->buildPayload($fieldData); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $defaultResponse = [ | ||||||||||||||||||||||||||||||
| 'success' => false, | ||||||||||||||||||||||||||||||
| // translators: %s is the plugin name. | ||||||||||||||||||||||||||||||
| 'message' => wp_sprintf(__('%s plugin is not installed or activated', 'bit-integrations'), 'Bit Integrations Pro'), | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| switch ($mainAction) { | ||||||||||||||||||||||||||||||
| case 'create_documentation': | ||||||||||||||||||||||||||||||
| $response = Hooks::apply(Config::withPrefix('wedocs_create_documentation'), $defaultResponse, $payload); | ||||||||||||||||||||||||||||||
| $actionType = 'create_documentation'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| case 'create_section': | ||||||||||||||||||||||||||||||
| $response = Hooks::apply(Config::withPrefix('wedocs_create_section'), $defaultResponse, $payload); | ||||||||||||||||||||||||||||||
| $actionType = 'create_section'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| case 'create_article': | ||||||||||||||||||||||||||||||
| $response = Hooks::apply(Config::withPrefix('wedocs_create_article'), $defaultResponse, $payload); | ||||||||||||||||||||||||||||||
| $actionType = 'create_article'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||
| $response = [ | ||||||||||||||||||||||||||||||
| 'success' => false, | ||||||||||||||||||||||||||||||
| 'message' => __('Invalid action', 'bit-integrations'), | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
| $actionType = 'unknown'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| $responseType = !empty($response['success']) ? 'success' : 'error'; | ||||||||||||||||||||||||||||||
| LogHandler::save($this->_integrationID, ['type' => 'weDocs', 'type_name' => $actionType], $responseType, $response); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return $response; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private function buildPayload($payload) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| if (!isset($payload['documentation_id']) && isset($this->_integrationDetails->selectedDocumentationId)) { | ||||||||||||||||||||||||||||||
| $payload['documentation_id'] = $this->_integrationDetails->selectedDocumentationId; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (!isset($payload['section_id']) && isset($this->_integrationDetails->selectedSectionId)) { | ||||||||||||||||||||||||||||||
| $payload['section_id'] = $this->_integrationDetails->selectedSectionId; | ||||||||||||||||||||||||||||||
|
Comment on lines
+85
to
+90
|
||||||||||||||||||||||||||||||
| if (!isset($payload['documentation_id']) && isset($this->_integrationDetails->selectedDocumentationId)) { | |
| $payload['documentation_id'] = $this->_integrationDetails->selectedDocumentationId; | |
| } | |
| if (!isset($payload['section_id']) && isset($this->_integrationDetails->selectedSectionId)) { | |
| $payload['section_id'] = $this->_integrationDetails->selectedSectionId; | |
| $selectedDocumentationId = $this->_integrationDetails->selectedDocumentationId ?? ''; | |
| if (!isset($payload['documentation_id']) && '' !== \trim((string) $selectedDocumentationId)) { | |
| $payload['documentation_id'] = absint($selectedDocumentationId); | |
| } | |
| $selectedSectionId = $this->_integrationDetails->selectedSectionId ?? ''; | |
| if (!isset($payload['section_id']) && '' !== \trim((string) $selectedSectionId)) { | |
| $payload['section_id'] = absint($selectedSectionId); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| if (!defined('ABSPATH')) { | ||
| exit; | ||
| } | ||
|
|
||
| use BitApps\Integrations\Actions\WeDocs\WeDocsController; | ||
| use BitApps\Integrations\Core\Util\Route; | ||
|
|
||
| Route::post('wedocs_authorize', [WeDocsController::class, 'weDocsAuthorize']); | ||
| Route::post('wedocs_get_documentations', [WeDocsController::class, 'getDocumentations']); | ||
| Route::post('wedocs_get_sections', [WeDocsController::class, 'getSections']); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <?php | ||
|
|
||
| namespace BitApps\Integrations\Actions\WeDocs; | ||
|
|
||
| use WP_Post; | ||
|
|
||
| /** | ||
| * Provide functionality for weDocs integration. | ||
| */ | ||
| class WeDocsController | ||
| { | ||
| private const DOC_POST_TYPE = 'docs'; | ||
|
|
||
| private const ALLOWED_POST_STATUSES = ['publish', 'draft', 'pending', 'private']; | ||
|
|
||
| public static function weDocsAuthorize() | ||
| { | ||
| self::checkPluginExists(); | ||
| wp_send_json_success(true); | ||
| } | ||
|
|
||
| public static function getDocumentations() | ||
| { | ||
| self::checkPluginExists(); | ||
|
|
||
| $allDocumentations = get_posts( | ||
| [ | ||
| 'post_type' => self::DOC_POST_TYPE, | ||
| 'post_status' => self::ALLOWED_POST_STATUSES, | ||
| 'posts_per_page' => -1, | ||
| 'post_parent' => 0, | ||
| 'orderby' => 'title', | ||
| 'order' => 'ASC', | ||
| ] | ||
| ); | ||
|
|
||
| $documentations = array_map( | ||
| function ($doc) { | ||
| return (object) [ | ||
| 'value' => (string) $doc->ID, | ||
| 'label' => $doc->post_title, | ||
| ]; | ||
| }, | ||
| $allDocumentations | ||
| ); | ||
|
|
||
| wp_send_json_success(['documentations' => $documentations], 200); | ||
| } | ||
|
|
||
| public static function getSections($request) | ||
| { | ||
| self::checkPluginExists(); | ||
|
|
||
| $documentationId = self::sanitizeId($request->documentation_id ?? ''); | ||
|
|
||
| if ($documentationId > 0) { | ||
| $documentation = get_post($documentationId); | ||
|
|
||
| if (!self::isValidDocumentation($documentation)) { | ||
| wp_send_json_error(__('Selected documentation is invalid.', 'bit-integrations'), 400); | ||
| } | ||
| } | ||
|
|
||
| $queryArgs = [ | ||
| 'post_type' => self::DOC_POST_TYPE, | ||
| 'post_status' => self::ALLOWED_POST_STATUSES, | ||
| 'posts_per_page' => -1, | ||
| 'orderby' => 'title', | ||
| 'order' => 'ASC', | ||
| ]; | ||
|
|
||
| if ($documentationId > 0) { | ||
| $queryArgs['post_parent'] = $documentationId; | ||
| } | ||
|
|
||
| $sections = get_posts($queryArgs); | ||
|
|
||
| if ($documentationId <= 0) { | ||
| $sections = array_values(array_filter($sections, [__CLASS__, 'isValidSection'])); | ||
| } | ||
|
Comment on lines
+72
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation to fetch all sections when no documentation ID is provided can be inefficient. It fetches all 'docs' posts and then filters them in PHP using A more efficient approach is to first fetch all top-level documentation IDs and then use With this change, the if ($documentationId > 0) {
$queryArgs['post_parent'] = $documentationId;
$sections = get_posts($queryArgs);
} else {
$documentationIds = get_posts([
'post_type' => self::DOC_POST_TYPE,
'post_status' => self::ALLOWED_POST_STATUSES,
'posts_per_page' => -1,
'post_parent' => 0,
'fields' => 'ids',
]);
if (empty($documentationIds)) {
$sections = [];
} else {
$queryArgs['post_parent__in'] = $documentationIds;
$sections = get_posts($queryArgs);
}
} |
||
|
|
||
| $options = array_map( | ||
| function ($section) { | ||
| return (object) [ | ||
| 'value' => (string) $section->ID, | ||
| 'label' => $section->post_title, | ||
| ]; | ||
| }, | ||
| $sections | ||
| ); | ||
|
|
||
| wp_send_json_success(['sections' => $options], 200); | ||
| } | ||
|
|
||
| public function execute($integrationData, $fieldValues) | ||
| { | ||
| $integrationDetails = $integrationData->flow_details; | ||
| $fieldMap = $integrationDetails->field_map ?? []; | ||
|
|
||
| $recordApiHelper = new RecordApiHelper($integrationDetails, $integrationData->id); | ||
|
|
||
| return $recordApiHelper->execute($fieldValues, $fieldMap); | ||
| } | ||
|
Comment on lines
+95
to
+103
|
||
|
|
||
| private static function checkPluginExists() | ||
| { | ||
| if (!class_exists('WeDocs')) { | ||
| wp_send_json_error(__('weDocs is not activated or not installed', 'bit-integrations'), 400); | ||
| } | ||
| } | ||
|
|
||
| private static function sanitizeId($value) | ||
| { | ||
| if (empty($value)) { | ||
| return 0; | ||
| } | ||
|
|
||
| return absint($value); | ||
| } | ||
|
|
||
| private static function isValidDocsPost($post) | ||
| { | ||
| return $post instanceof WP_Post && $post->post_type === self::DOC_POST_TYPE; | ||
| } | ||
|
|
||
| private static function isValidDocumentation($post) | ||
| { | ||
| return self::isValidDocsPost($post) && (int) $post->post_parent === 0; | ||
| } | ||
|
|
||
| private static function isValidSection($post) | ||
| { | ||
| if (!self::isValidDocsPost($post)) { | ||
| return false; | ||
| } | ||
|
|
||
| $parentId = (int) $post->post_parent; | ||
|
|
||
| if ($parentId <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| return (int) wp_get_post_parent_id($parentId) === 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -144,6 +144,7 @@ public static function allTriggersName() | |||
| 'WCSubscriptions' => ['name' => 'WooCommerce Subscriptions', 'isPro' => true, 'is_active' => false], | ||||
| 'Webhook' => ['name' => 'Webhook', 'isPro' => true, 'is_active' => false], | ||||
| 'WeForms' => ['name' => 'WeForms', 'isPro' => true, 'is_active' => false], | ||||
| 'WeDocs' => ['name' => 'weDocs', 'isPro' => true, 'is_active' => false], | ||||
|
||||
| 'WeDocs' => ['name' => 'weDocs', 'isPro' => true, 'is_active' => false], |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -91,6 +91,7 @@ export const customFormIntegrations = [ | |||
| 'SeoPress', | ||||
| 'ThriveLeads', | ||||
| 'NotificationX', | ||||
| 'WeDocs', | ||||
|
||||
| 'WeDocs', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better code clarity, consistency, and type safety, consider the following improvements:
$_integrationIDto$_integrationIdand the constructor parameter$integIdto$integrationIdto follow the camelCase convention used elsewhere.This change should also be propagated to where this property is used (lines 33 and 78).