790 lines
29 KiB
PHP
790 lines
29 KiB
PHP
<?php
|
|
/**
|
|
* مرجع API — OpenAPI از منبع اصلی، کش فایل، ایندکس سبک، REST
|
|
*
|
|
* @package Hesabix2
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/** @var string */
|
|
define('HESABIX2_API_DOCS_CACHE_DIR', 'hesabix-api-docs');
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_openapi_url() {
|
|
return apply_filters('hesabix2_api_docs_openapi_url', 'https://hsxn.hesabix.ir/openapi.json');
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_default_server() {
|
|
return apply_filters('hesabix2_api_docs_default_server', 'https://hsxn.hesabix.ir');
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_swagger_ui_url() {
|
|
return apply_filters('hesabix2_api_docs_swagger_ui_url', 'https://hsxn.hesabix.ir/docs');
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
function hesabix2_api_docs_cache_ttl() {
|
|
return (int) apply_filters('hesabix2_api_docs_cache_ttl', 30 * MINUTE_IN_SECONDS);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_page_url() {
|
|
static $cached = null;
|
|
if ($cached !== null) {
|
|
return $cached;
|
|
}
|
|
$pages = get_pages(
|
|
array(
|
|
'meta_key' => '_wp_page_template',
|
|
'meta_value' => 'page-api-reference.php',
|
|
'number' => 1,
|
|
)
|
|
);
|
|
$cached = !empty($pages)
|
|
? get_permalink($pages[0]->ID)
|
|
: home_url('/api/');
|
|
return apply_filters('hesabix2_api_docs_page_url', $cached);
|
|
}
|
|
|
|
/**
|
|
* گروهبندی تگها برای ناوبری
|
|
*
|
|
* @return array<int, array{id: string, label: string, tags: string[]}>
|
|
*/
|
|
function hesabix2_api_docs_tag_groups() {
|
|
$groups = array(
|
|
array(
|
|
'id' => 'start',
|
|
'label' => __('شروع و احراز هویت', 'hesabix2'),
|
|
'tags' => array('احراز هویت', 'health', 'کاربران', 'مدیریت سیستم'),
|
|
),
|
|
array(
|
|
'id' => 'business',
|
|
'label' => __('کسبوکار و تنظیمات', 'hesabix2'),
|
|
'tags' => array('کسبوکارها', 'سال مالی', 'مالیات', 'کیف پول', 'اعتبار'),
|
|
),
|
|
array(
|
|
'id' => 'operations',
|
|
'label' => __('عملیات و موجودی', 'hesabix2'),
|
|
'tags' => array('محصولات و کالاها', 'انبارداری', 'اشخاص و مشتریان', 'CRM', 'AI-CRM'),
|
|
),
|
|
array(
|
|
'id' => 'documents',
|
|
'label' => __('اسناد مالی', 'hesabix2'),
|
|
'tags' => array('اسناد فروش', 'اسناد خرید', 'اسناد انتقال', 'دریافت و پرداخت', 'مدیریت مالی', 'حسابداری'),
|
|
),
|
|
array(
|
|
'id' => 'reports',
|
|
'label' => __('گزارش و ارتباطات', 'hesabix2'),
|
|
'tags' => array('گزارشها', 'قالبهای گزارش', 'اطلاعرسانی', 'پشتیبانی', 'پشتیبانگیری'),
|
|
),
|
|
array(
|
|
'id' => 'platform',
|
|
'label' => __('پلتفرم و یکپارچهسازی', 'hesabix2'),
|
|
'tags' => array('یکپارچهسازی', 'فایل و ذخیرهسازی', 'هوش مصنوعی', 'workflows', 'distribution'),
|
|
),
|
|
);
|
|
return apply_filters('hesabix2_api_docs_tag_groups', $groups);
|
|
}
|
|
|
|
/**
|
|
* تگ پیشفرض هنگام ورود
|
|
*
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_default_tag() {
|
|
return apply_filters('hesabix2_api_docs_default_tag', 'احراز هویت');
|
|
}
|
|
|
|
/**
|
|
* @return string|false
|
|
*/
|
|
function hesabix2_api_docs_cache_dir() {
|
|
$upload = wp_upload_dir();
|
|
if (!empty($upload['error'])) {
|
|
return false;
|
|
}
|
|
$dir = trailingslashit($upload['basedir']) . HESABIX2_API_DOCS_CACHE_DIR;
|
|
if (!is_dir($dir)) {
|
|
wp_mkdir_p($dir);
|
|
}
|
|
if (!is_dir($dir) || !is_writable($dir)) {
|
|
return false;
|
|
}
|
|
$htaccess = $dir . '/.htaccess';
|
|
if (!file_exists($htaccess)) {
|
|
file_put_contents($htaccess, "Deny from all\n");
|
|
}
|
|
$index = $dir . '/index.php';
|
|
if (!file_exists($index)) {
|
|
file_put_contents($index, "<?php\n// Silence is golden.\n");
|
|
}
|
|
return $dir;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @return string|false
|
|
*/
|
|
function hesabix2_api_docs_cache_file($name) {
|
|
$dir = hesabix2_api_docs_cache_dir();
|
|
if (!$dir) {
|
|
return false;
|
|
}
|
|
return trailingslashit($dir) . sanitize_file_name($name);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $spec
|
|
* @return array<string, mixed>
|
|
*/
|
|
function hesabix2_api_docs_normalize_spec(array $spec) {
|
|
$base = untrailingslashit(hesabix2_api_docs_default_server());
|
|
$spec['servers'] = array(
|
|
array(
|
|
'url' => $base,
|
|
'description' => __('سرور production', 'hesabix2'),
|
|
),
|
|
);
|
|
return $spec;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $spec
|
|
* @return array<string, mixed>
|
|
*/
|
|
function hesabix2_api_docs_build_index(array $spec) {
|
|
$http_methods = array('get', 'post', 'put', 'patch', 'delete', 'head', 'options');
|
|
$operations = array();
|
|
$tag_counts = array();
|
|
$global_sec = !empty($spec['security']);
|
|
|
|
foreach ($spec['paths'] ?? array() as $path => $path_item) {
|
|
if ($path === '/' || $path === '') {
|
|
continue;
|
|
}
|
|
if (!is_array($path_item)) {
|
|
continue;
|
|
}
|
|
foreach ($http_methods as $method) {
|
|
if (empty($path_item[ $method ]) || !is_array($path_item[ $method ])) {
|
|
continue;
|
|
}
|
|
$op = $path_item[ $method ];
|
|
$tags = !empty($op['tags']) && is_array($op['tags']) ? $op['tags'] : array('other');
|
|
$id = !empty($op['operationId'])
|
|
? (string) $op['operationId']
|
|
: $method . '_' . preg_replace('/[^\w]+/', '_', $path);
|
|
|
|
$desc_plain = isset($op['description']) ? wp_strip_all_tags((string) $op['description']) : '';
|
|
$secured = $global_sec || !empty($op['security']);
|
|
|
|
$group_id = hesabix2_api_docs_resolve_tag_group($tags);
|
|
|
|
$operations[] = array(
|
|
'id' => $id,
|
|
'method' => $method,
|
|
'path' => $path,
|
|
'summary' => isset($op['summary']) ? (string) $op['summary'] : '',
|
|
'tags' => $tags,
|
|
'groupId' => $group_id,
|
|
'secured' => $secured,
|
|
'desc' => mb_substr($desc_plain, 0, 400),
|
|
);
|
|
|
|
foreach ($tags as $tag) {
|
|
$tag_counts[ $tag ] = isset($tag_counts[ $tag ] ) ? $tag_counts[ $tag ] + 1 : 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
usort(
|
|
$operations,
|
|
function ($a, $b) {
|
|
$pc = strcmp($a['path'], $b['path']);
|
|
return $pc !== 0 ? $pc : strcmp($a['method'], $b['method']);
|
|
}
|
|
);
|
|
|
|
$schema_names = array_keys($spec['components']['schemas'] ?? array());
|
|
sort($schema_names);
|
|
|
|
$info = $spec['info'] ?? array();
|
|
|
|
return array(
|
|
'openapi' => $spec['openapi'] ?? '3.0.0',
|
|
'info' => array(
|
|
'title' => $info['title'] ?? 'API',
|
|
'version' => $info['version'] ?? '',
|
|
'description' => $info['description'] ?? '',
|
|
),
|
|
'servers' => $spec['servers'] ?? array(),
|
|
'operations' => $operations,
|
|
'tagCounts' => $tag_counts,
|
|
'tagGroups' => hesabix2_api_docs_tag_groups(),
|
|
'defaultTag' => hesabix2_api_docs_default_tag(),
|
|
'schemaNames' => $schema_names,
|
|
'fetchedAt' => gmdate('c'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string[] $tags
|
|
* @return string
|
|
*/
|
|
function hesabix2_api_docs_resolve_tag_group(array $tags) {
|
|
$groups = hesabix2_api_docs_tag_groups();
|
|
foreach ($groups as $group) {
|
|
foreach ($group['tags'] as $gt) {
|
|
foreach ($tags as $tag) {
|
|
if ($tag === $gt || stripos($tag, $gt) !== false || stripos($gt, $tag) !== false) {
|
|
return $group['id'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 'other';
|
|
}
|
|
|
|
/**
|
|
* @param bool $force
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
function hesabix2_api_docs_fetch_spec($force = false) {
|
|
$transient_key = 'hesabix2_openapi_spec_v2';
|
|
$url = hesabix2_api_docs_openapi_url();
|
|
$ttl = hesabix2_api_docs_cache_ttl();
|
|
|
|
if (!$force) {
|
|
$file = hesabix2_api_docs_cache_file('openapi.json');
|
|
if ($file && is_readable($file) && (time() - filemtime($file)) < $ttl) {
|
|
$raw = file_get_contents($file);
|
|
$decoded = json_decode($raw, true);
|
|
if (is_array($decoded) && !empty($decoded['openapi'])) {
|
|
return $decoded;
|
|
}
|
|
}
|
|
$cached = get_transient($transient_key);
|
|
if (is_array($cached) && !empty($cached['openapi'])) {
|
|
return $cached;
|
|
}
|
|
}
|
|
|
|
$response = wp_remote_get(
|
|
$url,
|
|
array(
|
|
'timeout' => 90,
|
|
'sslverify' => true,
|
|
'headers' => array('Accept' => 'application/json'),
|
|
)
|
|
);
|
|
|
|
if (is_wp_error($response) || (int) wp_remote_retrieve_response_code($response) !== 200) {
|
|
$file = hesabix2_api_docs_cache_file('openapi.json');
|
|
if ($file && is_readable($file)) {
|
|
$decoded = json_decode((string) file_get_contents($file), true);
|
|
if (is_array($decoded)) {
|
|
return $decoded;
|
|
}
|
|
}
|
|
$cached = get_transient($transient_key);
|
|
return is_array($cached) ? $cached : null;
|
|
}
|
|
|
|
$spec = json_decode(wp_remote_retrieve_body($response), true);
|
|
if (!is_array($spec) || empty($spec['openapi'])) {
|
|
return null;
|
|
}
|
|
|
|
$spec = hesabix2_api_docs_normalize_spec($spec);
|
|
|
|
$json = wp_json_encode($spec);
|
|
$file = hesabix2_api_docs_cache_file('openapi.json');
|
|
if ($file && $json) {
|
|
file_put_contents($file, $json);
|
|
}
|
|
|
|
$index = hesabix2_api_docs_build_index($spec);
|
|
$index_file = hesabix2_api_docs_cache_file('index.json');
|
|
if ($index_file) {
|
|
file_put_contents($index_file, wp_json_encode($index));
|
|
}
|
|
|
|
set_transient($transient_key, $spec, $ttl);
|
|
delete_option('hesabix2_openapi_spec_stale');
|
|
|
|
return $spec;
|
|
}
|
|
|
|
/**
|
|
* @param bool $force
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
function hesabix2_api_docs_get_index($force = false) {
|
|
$ttl = hesabix2_api_docs_cache_ttl();
|
|
|
|
if (!$force) {
|
|
$file = hesabix2_api_docs_cache_file('index.json');
|
|
if ($file && is_readable($file) && (time() - filemtime($file)) < $ttl) {
|
|
$decoded = json_decode((string) file_get_contents($file), true);
|
|
if (is_array($decoded) && !empty($decoded['operations'])) {
|
|
return $decoded;
|
|
}
|
|
}
|
|
}
|
|
|
|
$spec = hesabix2_api_docs_fetch_spec($force);
|
|
if (!$spec) {
|
|
return null;
|
|
}
|
|
|
|
return hesabix2_api_docs_build_index($spec);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_api_docs_invalidate_cache() {
|
|
delete_transient('hesabix2_openapi_spec_v2');
|
|
foreach (array('openapi.json', 'index.json') as $name) {
|
|
$file = hesabix2_api_docs_cache_file($name);
|
|
if ($file && is_file($file)) {
|
|
wp_delete_file($file);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param WP_REST_Request $request
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
function hesabix2_api_docs_rest_index(WP_REST_Request $request) {
|
|
$refresh = (bool) $request->get_param('refresh');
|
|
if ($refresh && current_user_can('manage_options')) {
|
|
hesabix2_api_docs_invalidate_cache();
|
|
}
|
|
|
|
$index = hesabix2_api_docs_get_index($refresh && current_user_can('manage_options'));
|
|
if (!$index) {
|
|
return new WP_Error('openapi_index_unavailable', __('ایندکس API در دسترس نیست.', 'hesabix2'), array('status' => 502));
|
|
}
|
|
|
|
$response = new WP_REST_Response($index, 200);
|
|
$response->header('Cache-Control', 'public, max-age=120');
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param WP_REST_Request $request
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
function hesabix2_api_docs_rest_openapi(WP_REST_Request $request) {
|
|
$refresh = (bool) $request->get_param('refresh');
|
|
if ($refresh && current_user_can('manage_options')) {
|
|
hesabix2_api_docs_invalidate_cache();
|
|
}
|
|
|
|
$spec = hesabix2_api_docs_fetch_spec($refresh && current_user_can('manage_options'));
|
|
if (!$spec) {
|
|
return new WP_Error('openapi_unavailable', __('دریافت OpenAPI ممکن نشد.', 'hesabix2'), array('status' => 502));
|
|
}
|
|
|
|
$response = new WP_REST_Response($spec, 200);
|
|
$response->header('Cache-Control', 'public, max-age=300');
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param WP_REST_Request $request
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
function hesabix2_api_docs_rest_postman(WP_REST_Request $request) {
|
|
$spec = hesabix2_api_docs_fetch_spec(false);
|
|
if (!$spec) {
|
|
return new WP_Error('openapi_unavailable', __('OpenAPI در دسترس نیست.', 'hesabix2'), array('status' => 502));
|
|
}
|
|
|
|
$collection = hesabix2_api_docs_build_postman_collection($spec);
|
|
$response = new WP_REST_Response($collection, 200);
|
|
$response->header('Content-Type', 'application/json');
|
|
$response->header('Content-Disposition', 'attachment; filename="hesabix-api.postman_collection.json"');
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $spec
|
|
* @return array<string, mixed>
|
|
*/
|
|
function hesabix2_api_docs_build_postman_collection(array $spec) {
|
|
$base = hesabix2_api_docs_default_server();
|
|
$items = array();
|
|
$methods = array('get', 'post', 'put', 'patch', 'delete');
|
|
|
|
foreach ($spec['paths'] ?? array() as $path => $path_item) {
|
|
if (!is_array($path_item)) {
|
|
continue;
|
|
}
|
|
foreach ($methods as $method) {
|
|
if (empty($path_item[ $method ])) {
|
|
continue;
|
|
}
|
|
$op = $path_item[ $method ];
|
|
$items[] = array(
|
|
'name' => ($op['summary'] ?? $path) . ' [' . strtoupper($method) . ']',
|
|
'request' => array(
|
|
'method' => strtoupper($method),
|
|
'header' => array(
|
|
array('key' => 'Accept', 'value' => 'application/json'),
|
|
array('key' => 'Accept-Language', 'value' => 'fa'),
|
|
array('key' => 'X-Calendar-Type', 'value' => 'jalali'),
|
|
array('key' => 'Authorization', 'value' => 'ApiKey {{api_key}}'),
|
|
),
|
|
'url' => array(
|
|
'raw' => rtrim($base, '/') . $path,
|
|
'host' => array(parse_url($base, PHP_URL_HOST)),
|
|
'path' => array_values(array_filter(explode('/', trim($path, '/')))),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
return array(
|
|
'info' => array(
|
|
'name' => 'Hesabix API',
|
|
'description' => $spec['info']['description'] ?? '',
|
|
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
|
|
),
|
|
'variable' => array(
|
|
array('key' => 'api_key', 'value' => 'ak_live_YOUR_KEY'),
|
|
array('key' => 'base_url', 'value' => $base),
|
|
),
|
|
'item' => $items,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param WP_REST_Request $request
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
function hesabix2_api_docs_rest_webhook(WP_REST_Request $request) {
|
|
$secret = apply_filters('hesabix2_api_docs_webhook_secret', get_option('hesabix2_api_docs_webhook_secret'));
|
|
if (!$secret) {
|
|
$secret = wp_generate_password(32, false);
|
|
update_option('hesabix2_api_docs_webhook_secret', $secret, false);
|
|
}
|
|
|
|
$provided = $request->get_header('x_hesabix_docs_secret');
|
|
if (!$provided || !hash_equals($secret, $provided)) {
|
|
return new WP_Error('forbidden', __('Unauthorized', 'hesabix2'), array('status' => 403));
|
|
}
|
|
|
|
hesabix2_api_docs_invalidate_cache();
|
|
hesabix2_api_docs_fetch_spec(true);
|
|
|
|
return new WP_REST_Response(
|
|
array(
|
|
'success' => true,
|
|
'message' => __('کش مستندات API بهروزرسانی شد.', 'hesabix2'),
|
|
),
|
|
200
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_api_docs_register_rest() {
|
|
register_rest_route(
|
|
'hesabix/v1',
|
|
'/openapi-index',
|
|
array(
|
|
'methods' => 'GET',
|
|
'permission_callback' => '__return_true',
|
|
'callback' => 'hesabix2_api_docs_rest_index',
|
|
'args' => array(
|
|
'refresh' => array(
|
|
'type' => 'boolean',
|
|
'default' => false,
|
|
'sanitize_callback' => 'rest_sanitize_boolean',
|
|
),
|
|
),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
'hesabix/v1',
|
|
'/openapi',
|
|
array(
|
|
'methods' => 'GET',
|
|
'permission_callback' => '__return_true',
|
|
'callback' => 'hesabix2_api_docs_rest_openapi',
|
|
'args' => array(
|
|
'refresh' => array(
|
|
'type' => 'boolean',
|
|
'default' => false,
|
|
'sanitize_callback' => 'rest_sanitize_boolean',
|
|
),
|
|
),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
'hesabix/v1',
|
|
'/postman',
|
|
array(
|
|
'methods' => 'GET',
|
|
'permission_callback' => '__return_true',
|
|
'callback' => 'hesabix2_api_docs_rest_postman',
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
'hesabix/v1',
|
|
'/openapi-invalidate',
|
|
array(
|
|
'methods' => 'POST',
|
|
'permission_callback' => '__return_true',
|
|
'callback' => 'hesabix2_api_docs_rest_webhook',
|
|
)
|
|
);
|
|
}
|
|
add_action('rest_api_init', 'hesabix2_api_docs_register_rest');
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_api_docs_enqueue_assets() {
|
|
if (!is_page_template('page-api-reference.php')) {
|
|
return;
|
|
}
|
|
|
|
$theme_uri = get_template_directory_uri();
|
|
$theme_dir = get_template_directory();
|
|
$css = $theme_dir . '/assets/css/api-docs.css';
|
|
$js = $theme_dir . '/assets/js/api-docs.js';
|
|
|
|
wp_enqueue_style(
|
|
'hesabix2-api-docs',
|
|
$theme_uri . '/assets/css/api-docs.css',
|
|
array('hesabix2-style'),
|
|
file_exists($css) ? (string) filemtime($css) : '2.0'
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'hesabix2-api-docs',
|
|
$theme_uri . '/assets/js/api-docs.js',
|
|
array(),
|
|
file_exists($js) ? (string) filemtime($js) : '2.0',
|
|
true
|
|
);
|
|
|
|
$webhook_secret = get_option('hesabix2_api_docs_webhook_secret', '');
|
|
|
|
wp_localize_script(
|
|
'hesabix2-api-docs',
|
|
'hesabixApiDocs',
|
|
array(
|
|
'indexUrl' => rest_url('hesabix/v1/openapi-index'),
|
|
'specUrl' => rest_url('hesabix/v1/openapi'),
|
|
'specFallbackUrl' => hesabix2_api_docs_openapi_url(),
|
|
'postmanUrl' => rest_url('hesabix/v1/postman'),
|
|
'defaultServer' => hesabix2_api_docs_default_server(),
|
|
'swaggerUrl' => hesabix2_api_docs_swagger_ui_url(),
|
|
'pageUrl' => hesabix2_api_docs_page_url(),
|
|
'docsUrl' => function_exists('hesabix2_fp_docs_url') ? hesabix2_fp_docs_url() : home_url('/docs/'),
|
|
'defaultTag' => hesabix2_api_docs_default_tag(),
|
|
'tagGroups' => hesabix2_api_docs_tag_groups(),
|
|
'canRefresh' => current_user_can('manage_options'),
|
|
'refreshIndexUrl' => add_query_arg('refresh', '1', rest_url('hesabix/v1/openapi-index')),
|
|
'refreshSpecUrl' => add_query_arg('refresh', '1', rest_url('hesabix/v1/openapi')),
|
|
'webhookUrl' => rest_url('hesabix/v1/openapi-invalidate'),
|
|
'webhookSecret' => current_user_can('manage_options') ? $webhook_secret : '',
|
|
'listPageSize' => 60,
|
|
'i18n' => array(
|
|
'loading' => __('در حال بارگذاری…', 'hesabix2'),
|
|
'loadingSpec' => __('در حال بارگذاری جزئیات کامل…', 'hesabix2'),
|
|
'loadError' => __('بارگذاری مستندات ناموفق بود.', 'hesabix2'),
|
|
'retry' => __('تلاش مجدد', 'hesabix2'),
|
|
'searchPlaceholder' => __('جستجو در مسیر، خلاصه، توضیح یا شناسه…', 'hesabix2'),
|
|
'searchShortcut' => __('Ctrl+K', 'hesabix2'),
|
|
'allTags' => __('همهٔ عملیات', 'hesabix2'),
|
|
'otherTags' => __('سایر بخشها', 'hesabix2'),
|
|
'parameters' => __('پارامترها', 'hesabix2'),
|
|
'requestBody' => __('بدنهٔ درخواست', 'hesabix2'),
|
|
'responses' => __('پاسخها', 'hesabix2'),
|
|
'execute' => __('ارسال', 'hesabix2'),
|
|
'authorize' => __('کلید API', 'hesabix2'),
|
|
'authorizeActive' => __('کلید فعال', 'hesabix2'),
|
|
'authorizeHint' => __('فرمت: ApiKey ak_live_…', 'hesabix2'),
|
|
'authorizeSave' => __('ذخیره در این مرورگر', 'hesabix2'),
|
|
'authorizeSession' => __('فقط این نشست (session)', 'hesabix2'),
|
|
'authorizeClear' => __('پاک کردن', 'hesabix2'),
|
|
'baseUrl' => __('آدرس پایه', 'hesabix2'),
|
|
'copyCurl' => __('کپی curl', 'hesabix2'),
|
|
'copied' => __('کپی شد', 'hesabix2'),
|
|
'noResults' => __('نتیجهای یافت نشد.', 'hesabix2'),
|
|
'selectOperation' => __('یک عملیات را انتخاب کنید.', 'hesabix2'),
|
|
'securityWarning' => __('کلید API را فقط در محیط امن وارد کنید.', 'hesabix2'),
|
|
'fromCache' => __('همگام · کش سرور', 'hesabix2'),
|
|
'fromSource' => __('همگام · منبع اصلی', 'hesabix2'),
|
|
'endpoints' => __('عملیات', 'hesabix2'),
|
|
'tags' => __('بخش', 'hesabix2'),
|
|
'secured' => __('نیاز به کلید API', 'hesabix2'),
|
|
'public' => __('عمومی / مهمان', 'hesabix2'),
|
|
'corsError' => __('احتمالاً محدودیت CORS مانع درخواست از مرورگر شده است. از curl یا Swagger UI روی سرور API استفاده کنید.', 'hesabix2'),
|
|
'headers' => __('هدرهای درخواست', 'hesabix2'),
|
|
'businessId' => __('X-Business-ID', 'hesabix2'),
|
|
'language' => __('Accept-Language', 'hesabix2'),
|
|
'calendar' => __('X-Calendar-Type', 'hesabix2'),
|
|
'codeSamples' => __('نمونه کد', 'hesabix2'),
|
|
'schemas' => __('مدلهای داده', 'hesabix2'),
|
|
'exportPostman' => __('دانلود Postman', 'hesabix2'),
|
|
'quickStart' => __('شروع سریع', 'hesabix2'),
|
|
'step1' => __('کپچا / تنظیمات عمومی', 'hesabix2'),
|
|
'step2' => __('ورود و دریافت api_key', 'hesabix2'),
|
|
'step3' => __('فراخوانی با Authorization: ApiKey …', 'hesabix2'),
|
|
'showing' => __('نمایش', 'hesabix2'),
|
|
'of' => __('از', 'hesabix2'),
|
|
'loadMore' => __('بارگذاری بیشتر', 'hesabix2'),
|
|
'schemaSearch' => __('جستجو در مدلها…', 'hesabix2'),
|
|
'adminRefresh' => __('بهروزرسانی کش', 'hesabix2'),
|
|
'adminWebhook' => __('URL وبهوک invalidation', 'hesabix2'),
|
|
'tryReadOnly' => __('حالت فقط مشاهده (بدون ارسال)', 'hesabix2'),
|
|
),
|
|
)
|
|
);
|
|
}
|
|
add_action('wp_enqueue_scripts', 'hesabix2_api_docs_enqueue_assets');
|
|
|
|
/**
|
|
* meta صفحه API → inc/seo.php (hesabix2_seo_api_docs_head)
|
|
*/
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_maybe_create_api_docs_page() {
|
|
if (get_option('hesabix2_api_docs_page_created')) {
|
|
return;
|
|
}
|
|
$existing = get_pages(
|
|
array(
|
|
'meta_key' => '_wp_page_template',
|
|
'meta_value' => 'page-api-reference.php',
|
|
'number' => 1,
|
|
)
|
|
);
|
|
if (!empty($existing)) {
|
|
update_option('hesabix2_api_docs_page_created', 1);
|
|
return;
|
|
}
|
|
$page_id = wp_insert_post(
|
|
array(
|
|
'post_title' => __('مرجع API', 'hesabix2'),
|
|
'post_name' => 'api',
|
|
'post_status' => 'publish',
|
|
'post_type' => 'page',
|
|
'post_content' => '',
|
|
),
|
|
true
|
|
);
|
|
if (!is_wp_error($page_id) && $page_id) {
|
|
update_post_meta($page_id, '_wp_page_template', 'page-api-reference.php');
|
|
update_option('hesabix2_api_docs_page_created', 1);
|
|
}
|
|
}
|
|
add_action('after_setup_theme', 'hesabix2_maybe_create_api_docs_page', 35);
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_api_docs_maybe_warm_cache() {
|
|
if (!get_option('hesabix2_api_docs_page_created')) {
|
|
return;
|
|
}
|
|
$last = (int) get_option('hesabix2_openapi_warm_at', 0);
|
|
if ($last && (time() - $last) < DAY_IN_SECONDS) {
|
|
return;
|
|
}
|
|
hesabix2_api_docs_get_index(false);
|
|
update_option('hesabix2_openapi_warm_at', time(), false);
|
|
}
|
|
add_action('init', 'hesabix2_api_docs_maybe_warm_cache', 99);
|
|
|
|
/**
|
|
* کارت مرجع API در صفحهٔ اصلی WeDocs
|
|
*
|
|
* @return void
|
|
*/
|
|
function hesabix2_wedocs_api_developer_card() {
|
|
if (!function_exists('wedocs_get_general_settings')) {
|
|
return;
|
|
}
|
|
$docs_home = wedocs_get_general_settings('docs_home', '');
|
|
if (!$docs_home || !is_page((int) $docs_home)) {
|
|
return;
|
|
}
|
|
$api_url = hesabix2_api_docs_page_url();
|
|
?>
|
|
<aside class="hesabix-wedocs-api-card mb-10 rounded-3xl border border-brand-500/25 bg-gradient-to-l from-brand-500/10 via-white to-accent-500/5 p-6 sm:p-8 shadow-sm" aria-label="<?php esc_attr_e('مستندات توسعهدهندگان', 'hesabix2'); ?>">
|
|
<div class="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4">
|
|
<div class="text-center lg:text-right">
|
|
<p class="text-brand-600 text-sm font-semibold mb-1"><?php esc_html_e('توسعهدهندگان', 'hesabix2'); ?></p>
|
|
<h2 class="text-xl font-bold text-slate-800 mb-2"><?php esc_html_e('مرجع API و یکپارچهسازی', 'hesabix2'); ?></h2>
|
|
<p class="text-slate-600 text-sm mb-0 max-w-xl"><?php esc_html_e('مستندات فنی REST، اجرای درخواست، نمونه کد و خروجی Postman — همیشه همگام با OpenAPI رسمی.', 'hesabix2'); ?></p>
|
|
</div>
|
|
<div class="flex flex-wrap justify-center lg:justify-end gap-2 shrink-0">
|
|
<a href="<?php echo esc_url($api_url); ?>" class="inline-flex items-center justify-center px-5 py-2.5 rounded-xl bg-brand-500 text-white font-semibold text-sm hover:bg-brand-600 transition-colors">
|
|
<?php esc_html_e('ورود به مرجع API', 'hesabix2'); ?>
|
|
</a>
|
|
<a href="<?php echo esc_url($api_url . '#quick-start'); ?>" class="inline-flex items-center justify-center px-5 py-2.5 rounded-xl border border-brand-500/40 text-brand-600 font-semibold text-sm hover:bg-brand-500/10 transition-colors">
|
|
<?php esc_html_e('شروع سریع', 'hesabix2'); ?>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</aside>
|
|
<?php
|
|
}
|
|
/**
|
|
* جزئیات وبهوک برای مدیر در پایین صفحه API
|
|
*
|
|
* @return void
|
|
*/
|
|
function hesabix2_api_docs_admin_footer() {
|
|
if (!is_page_template('page-api-reference.php') || !current_user_can('manage_options')) {
|
|
return;
|
|
}
|
|
$secret = get_option('hesabix2_api_docs_webhook_secret');
|
|
if (!$secret) {
|
|
$secret = wp_generate_password(32, false);
|
|
update_option('hesabix2_api_docs_webhook_secret', $secret, false);
|
|
}
|
|
$url = rest_url('hesabix/v1/openapi-invalidate');
|
|
?>
|
|
<div class="<?php echo esc_attr('max-w-[1600px] mx-auto px-4 sm:px-6 lg:px-8'); ?> pb-8">
|
|
<details class="api-docs-admin-details text-xs text-slate-500">
|
|
<summary class="cursor-pointer font-medium text-slate-600"><?php esc_html_e('تنظیمات مدیر — invalidation کش', 'hesabix2'); ?></summary>
|
|
<p class="mt-2 mb-1"><?php esc_html_e('پس از deploy API، POST با هدر X-Hesabix-Docs-Secret:', 'hesabix2'); ?></p>
|
|
<code class="block p-2 bg-white border border-slate-200 rounded-lg break-all" dir="ltr"><?php echo esc_html($url); ?></code>
|
|
<p class="mt-2 mb-0" dir="ltr"><code><?php echo esc_html($secret); ?></code></p>
|
|
</details>
|
|
</div>
|
|
<?php
|
|
}
|
|
add_action('wp_footer', 'hesabix2_api_docs_admin_footer', 20);
|