/* 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 . '
oembed_invalid_url