/* Plugin Name: PNSG PWA Enabler Description: Enable PWA for the website. Version: 1.4 Author: Peyvandnegar Software Group Text Domain: pnsg-pwa-enabler */ require_once plugin_dir_path(__FILE__) . 'includes/pnsg-pwa-settings.php'; // اضافه کردن manifest به head سایت function pnsg_pwa_add_manifest() { echo ''; } add_action('wp_head', 'pnsg_pwa_add_manifest'); // اضافه کردن سرویس‌ورکر به سایت function pnsg_pwa_add_service_worker() { if (!is_admin()) { echo ''; } } add_action('wp_footer', 'pnsg_pwa_add_service_worker'); // ایجاد فایل‌های manifest و آیکن‌ها function pnsg_pwa_create_manifest_and_icons() { $site_name = get_option('pnsg_pwa_site_name', get_bloginfo('name')); $site_short_name = get_option('pnsg_pwa_site_short_name', mb_substr($site_name, 0, 12)); $icon_id = get_option('site_icon'); if (!$icon_id) { pnsg_pwa_admin_notice(__('Site icon not set. Please set a site icon for PWA functionality.', 'pnsg-pwa-enabler'), 'error'); return; } $icon_url = wp_get_attachment_image_url($icon_id, 'full'); if (!$icon_url) { pnsg_pwa_admin_notice(__('Failed to retrieve site icon URL. Please try again.', 'pnsg-pwa-enabler'), 'error'); return; } $icon_path = str_replace(site_url('/'), ABSPATH, $icon_url); $manifest_dir = plugin_dir_path(__FILE__) . 'manifest'; $icons_dir = plugin_dir_path(__FILE__) . 'assets/icons'; if (!file_exists($manifest_dir)) { wp_mkdir_p($manifest_dir); } if (!file_exists($icons_dir)) { wp_mkdir_p($icons_dir); } $sizes = [192, 512]; foreach ($sizes as $size) { $image = wp_get_image_editor($icon_path); if (!is_wp_error($image)) { $image->resize($size, $size, true); $result = $image->save($icons_dir . '/icon-' . $size . 'x' . $size . '.png'); if (is_wp_error($result)) { error_log('Failed to save resized icon: ' . $size); } } else { error_log('Failed to create image editor.'); } } $manifest = [ 'name' => $site_name, 'short_name' => $site_short_name, 'start_url' => '/', 'display' => 'standalone', 'background_color' => '#ffffff', 'theme_color' => '#d58e24', 'icons' => [ [ 'src' => plugin_dir_url(__FILE__) . 'assets/icons/icon-192x192.png', 'sizes' => '192x192', 'type' => 'image/png', ], [ 'src' => plugin_dir_url(__FILE__) . 'assets/icons/icon-512x512.png', 'sizes' => '512x512', 'type' => 'image/png', ], ], ]; $manifest_path = $manifest_dir . '/manifest.json'; file_put_contents($manifest_path, json_encode($manifest)); } // نمایش پیام در پیشخوان function pnsg_pwa_admin_notice($message, $type = 'success') { add_action('admin_notices', function() use ($message, $type) { echo '

' . $message . '

'; }); } // اجرای تابع هنگام فعال‌سازی افزونه function pnsg_pwa_activate() { $site_name = get_bloginfo('name'); $site_short_name = mb_substr($site_name, 0, 12); add_option('pnsg_pwa_site_name', $site_name); add_option('pnsg_pwa_site_short_name', $site_short_name); add_option('pnsg_pwa_enabled', true); pnsg_pwa_create_manifest_and_icons(); update_option('pnsg_pwa_show_activation_notice', true); } register_activation_hook(__FILE__, 'pnsg_pwa_activate'); // نمایش پیام فعال‌سازی در اولین بار بارگذاری پیشخوان function pnsg_pwa_show_activation_notice() { if (get_option('pnsg_pwa_show_activation_notice')) { pnsg_pwa_admin_notice(__('PWA setup completed successfully!', 'pnsg-pwa-enabler')); delete_option('pnsg_pwa_show_activation_notice'); } } add_action('admin_init', 'pnsg_pwa_show_activation_notice'); // پاکسازی وضعیت هنگام تغییر آیکن سایت function pnsg_pwa_reset_manifest_status() { pnsg_pwa_create_manifest_and_icons(); update_option('pnsg_pwa_show_reset_notice', true); } add_action('customize_save_after', 'pnsg_pwa_reset_manifest_status'); add_action('update_option_site_icon', 'pnsg_pwa_reset_manifest_status'); // نمایش پیام هنگام تغییر آیکن سایت function pnsg_pwa_show_reset_notice() { if (get_option('pnsg_pwa_show_reset_notice')) { pnsg_pwa_admin_notice(__('PWA manifest and icons updated.', 'pnsg-pwa-enabler'), 'info'); delete_option('pnsg_pwa_show_reset_notice'); } } add_action('admin_init', 'pnsg_pwa_show_reset_notice'); // تابع برای حذف فایل‌های آیکن و manifest function pnsg_pwa_delete_files() { $manifest_path = plugin_dir_path(__FILE__) . 'manifest/manifest.json'; $icons_dir = plugin_dir_path(__FILE__) . 'assets/icons/'; if (file_exists($manifest_path)) { unlink($manifest_path); } $sizes = [192, 512]; foreach ($sizes as $size) { $icon_path = $icons_dir . 'icon-' . $size . 'x' . $size . '.png'; if (file_exists($icon_path)) { unlink($icon_path); } } delete_option('pnsg_pwa_site_name'); delete_option('pnsg_pwa_site_short_name'); delete_option('pnsg_pwa_enabled'); } register_deactivation_hook(__FILE__, 'pnsg_pwa_delete_files'); oembed_invalid_urlNot Found404