diff --git a/wordpress/svn/trunk/assets/icon-128x128.png b/wordpress/svn/trunk/assets/icon-128x128.png new file mode 100644 index 0000000..9692f21 Binary files /dev/null and b/wordpress/svn/trunk/assets/icon-128x128.png differ diff --git a/wordpress/svn/trunk/simplepdf-embed.php b/wordpress/svn/trunk/simplepdf-embed.php index fef9c84..d149c00 100644 --- a/wordpress/svn/trunk/simplepdf-embed.php +++ b/wordpress/svn/trunk/simplepdf-embed.php @@ -12,82 +12,958 @@ if ( ! defined( 'ABSPATH' ) ) exit; -function simplepdf_settings_init() { - add_option('simplepdf_company_identifier'); +define('SIMPLEPDF_PLUGIN_VERSION', '1.1.3'); +define('SIMPLEPDF_SETTINGS_SCREEN', 'settings_page_simplepdf_settings'); +define('SIMPLEPDF_POST_LIST_LIMIT', 300); +define('SIMPLEPDF_PDF_PAGE_LIMIT', 100); +define('SIMPLEPDF_WEB_EMBED_VERSION', '1.8.4'); +define('SIMPLEPDF_REVIEW_URL', 'https://wordpress.org/support/plugin/simplepdf-embed/reviews/#new-post'); +define('SIMPLEPDF_PRICING_URL', 'https://simplepdf.com/pricing?ref=wordpress'); +function simplepdf_settings_init() { add_submenu_page( 'options-general.php', - 'SimplePDF Embed Settings', - 'SimplePDF Embed', + __('SimplePDF Embed Settings', 'simplepdf-embed'), + __('SimplePDF Embed', 'simplepdf-embed'), 'manage_options', 'simplepdf_settings', 'simplepdf_settings_page' ); +} + +function simplepdf_register_settings() { + register_setting('simplepdf_settings', 'simplepdf_company_identifier', array( + 'sanitize_callback' => 'simplepdf_sanitize_company_identifier', + )); + register_setting('simplepdf_settings', 'simplepdf_load_scope', array( + 'sanitize_callback' => 'simplepdf_sanitize_load_scope', + )); + register_setting('simplepdf_settings', 'simplepdf_selected_post_ids', array( + 'sanitize_callback' => 'simplepdf_sanitize_selected_post_ids', + )); +} + +function simplepdf_sanitize_company_identifier($value) { + $subdomain = preg_replace('#^(?:https?://)?(?:[^/]*\.)?([a-z0-9-]+)\.simplepdf\.com.*$#i', '$1', trim((string) $value)); + + return preg_replace('/[^a-z0-9-]/', '', strtolower($subdomain)); +} + +function simplepdf_sanitize_load_scope($value) { + return $value === 'selected' ? 'selected' : 'everywhere'; +} + +function simplepdf_sanitize_selected_post_ids($value) { + if ( ! is_array($value) ) { + return array(); + } + + return array_values(array_unique(array_filter(array_map('absint', $value)))); +} + +function simplepdf_get_company_identifier() { + return simplepdf_sanitize_company_identifier(get_option('simplepdf_company_identifier')); +} + +function simplepdf_fetch_account_status($company_identifier) { + $response = wp_remote_head('https://' . $company_identifier . '.simplepdf.com/', array( + 'redirection' => 0, + 'timeout' => 5, + )); + if ( is_wp_error($response) ) { + return 'unreachable'; + } + + $status_code = (int) wp_remote_retrieve_response_code($response); + if ( $status_code === 404 ) { + return 'not_found'; + } + + return $status_code >= 200 && $status_code < 400 ? 'found' : 'unreachable'; +} + +function simplepdf_get_account_status($company_identifier) { + $cached_check = get_transient('simplepdf_account_check'); + $is_cached = is_array($cached_check) + && isset($cached_check['company_identifier'], $cached_check['status']) + && $cached_check['company_identifier'] === $company_identifier; + if ( $is_cached ) { + return $cached_check['status']; + } + + $account_status = simplepdf_fetch_account_status($company_identifier); + $cache_duration = $account_status === 'found' ? HOUR_IN_SECONDS : 5 * MINUTE_IN_SECONDS; + set_transient('simplepdf_account_check', array( + 'company_identifier' => $company_identifier, + 'status' => $account_status, + ), $cache_duration); + + return $account_status; +} + +function simplepdf_get_load_scope() { + return simplepdf_sanitize_load_scope(get_option('simplepdf_load_scope')); +} + +function simplepdf_get_selected_post_ids() { + return array_map('absint', (array) get_option('simplepdf_selected_post_ids', array())); +} + +function simplepdf_should_load_on_current_request() { + if ( simplepdf_get_load_scope() === 'everywhere' ) { + return true; + } + + $queried_object = get_queried_object(); + + return $queried_object instanceof WP_Post + && in_array($queried_object->ID, simplepdf_get_selected_post_ids(), true); +} + +function simplepdf_enqueue_script() { + if ( ! simplepdf_should_load_on_current_request() ) { + return; + } + + $script_src = plugin_dir_url(__FILE__) . 'build/web-embed-pdf.js'; + + wp_enqueue_script('simplepdf-web-embed-pdf', $script_src, array(), SIMPLEPDF_WEB_EMBED_VERSION, true); + + $saved_company_identifier = simplepdf_get_company_identifier(); + $company_identifier = $saved_company_identifier === '' ? 'wordpress' : $saved_company_identifier; + $inline_script = "window.simplePDF.setConfig({ companyIdentifier: '" . esc_js($company_identifier) . "' });"; + + wp_add_inline_script('simplepdf-web-embed-pdf', $inline_script, 'after'); +} + +function simplepdf_should_show_review_notice() { + $company_identifier = simplepdf_get_company_identifier(); + + $is_eligible = $company_identifier !== '' + && ! get_option('simplepdf_review_notice_dismissed') + && simplepdf_get_account_status($company_identifier) === 'found'; + if ( ! $is_eligible ) { + return false; + } + + $report = simplepdf_get_pdf_link_report(); + + return $report['counts']['simplepdf'] > 0; +} + +function simplepdf_enqueue_admin_assets($hook_suffix) { + if ( $hook_suffix !== SIMPLEPDF_SETTINGS_SCREEN ) { + return; + } + wp_register_style('simplepdf-settings', false, array(), SIMPLEPDF_PLUGIN_VERSION); + wp_enqueue_style('simplepdf-settings'); + wp_add_inline_style('simplepdf-settings', simplepdf_admin_css()); - add_settings_section( - 'simplepdf_settings_section', - '', - 'simplepdf_settings_section_callback', - 'simplepdf_settings' + wp_register_script('simplepdf-review-notice', false, array(), SIMPLEPDF_PLUGIN_VERSION, true); + wp_enqueue_script('simplepdf-review-notice'); + wp_add_inline_script('simplepdf-review-notice', simplepdf_review_notice_script()); +} + +function simplepdf_review_notice_script() { + $dismiss_request = array( + 'url' => admin_url('admin-ajax.php'), + 'action' => 'simplepdf_dismiss_review_notice', + 'nonce' => wp_create_nonce('simplepdf_dismiss_review_notice'), ); - add_settings_field( - 'simplepdf_company_identifier', - 'Company Identifier', - 'simplepdf_company_identifier_callback', - 'simplepdf_settings', - 'simplepdf_settings_section' + return 'const simplepdfReviewNotice = ' . wp_json_encode($dismiss_request) . ';' . <<<'JS' + +document.addEventListener('click', (event) => { + const notice = event.target.closest('.simplepdf-review-notice'); + const isDismissal = notice !== null && event.target.closest('.notice-dismiss, .simplepdf-review-link') !== null; + if (!isDismissal) { + return; + } + + const body = new URLSearchParams({ action: simplepdfReviewNotice.action, _ajax_nonce: simplepdfReviewNotice.nonce }); + fetch(simplepdfReviewNotice.url, { method: 'POST', credentials: 'same-origin', body }); + if (event.target.closest('.simplepdf-review-link') !== null) { + notice.remove(); + } +}); +JS; +} + +function simplepdf_render_review_notice() { + $screen = get_current_screen(); + $is_settings_screen = $screen !== null && $screen->id === SIMPLEPDF_SETTINGS_SCREEN; + if ( ! $is_settings_screen || ! simplepdf_should_show_review_notice() ) { + return; + } + ?> +
+ + + + + +
+Unlock Exclusive Features - Get Your Company Identifier Now: Dive deeper at SimplePDF/embed
'; - echo 'Have questions? Reach out for personalized support at support@simplepdf.com - We\'re here to help you succeed!
'; +// Mirrors getSimplePDFElements in the bundled web-embed script, so the table predicts what visitors get +// CF: src/shared.ts +function simplepdf_classify_link($href, $classes) { + if ( in_array('exclude-simplepdf', $classes, true) ) { + return 'excluded'; + } + + $is_pdf_link = substr(strtolower($href), -4) === '.pdf' + || substr(strtolower((string) wp_parse_url($href, PHP_URL_PATH)), -4) === '.pdf'; + $is_simplepdf_link = preg_match('#^https://[^.]+\.simplepdf\.com(/[^/]+)?/(form|documents)/.+#', $href) === 1; + if ( $is_pdf_link || $is_simplepdf_link || in_array('simplepdf', $classes, true) ) { + return 'opens_in_simplepdf'; + } + + return 'not_recognised'; } -function enqueue_simplepdf_script() { +function simplepdf_extract_pdf_links($post_content) { + preg_match_all('/]*>/i', $post_content, $anchor_matches); + + $pdf_links = array(); + foreach ( $anchor_matches[0] as $anchor_tag ) { + $href = simplepdf_get_anchor_attribute($anchor_tag, 'href'); + $classes = preg_split('/\s+/', simplepdf_get_anchor_attribute($anchor_tag, 'class'), -1, PREG_SPLIT_NO_EMPTY); + $link_type = simplepdf_classify_link($href, $classes); + $mentions_pdf = stripos($href, '.pdf') !== false; + if ( $link_type === 'not_recognised' && ! $mentions_pdf ) { + continue; + } - $plugin_url = plugin_dir_url(__FILE__); - $script_src = $plugin_url . 'build/web-embed-pdf.js'; + $pdf_links[] = array( + 'href' => $href, + 'link_type' => $link_type, + ); + } - wp_enqueue_script('simplepdf-web-embed-pdf', $script_src, array(), '1.8.4', true); + return $pdf_links; +} - $company_identifier = get_option('simplepdf_company_identifier'); - $companyIdentifier = empty($company_identifier) ? 'wordpress' : $company_identifier; - $inline_script = "window.simplePDF.setConfig({ companyIdentifier: '" . esc_js($companyIdentifier) . "' });"; +function simplepdf_scan_pages_with_pdf_links() { + global $wpdb; - wp_add_inline_script('simplepdf-web-embed-pdf', $inline_script, 'after'); + $post_ids = $wpdb->get_col($wpdb->prepare( + "SELECT ID FROM {$wpdb->posts} + WHERE post_type IN ('page', 'post') + AND post_status IN ('publish', 'private', 'draft') + AND (post_content LIKE %s OR post_content LIKE %s) + ORDER BY post_modified DESC + LIMIT %d", + '%' . $wpdb->esc_like('.pdf') . '%', + '%' . $wpdb->esc_like('simplepdf') . '%', + SIMPLEPDF_PDF_PAGE_LIMIT + )); + if ( empty($post_ids) ) { + return array('pages' => array(), 'is_capped' => false); + } + + $posts = get_posts(array( + 'post__in' => array_map('absint', $post_ids), + 'post_type' => array('page', 'post'), + 'post_status' => array('publish', 'private', 'draft'), + 'orderby' => 'post__in', + 'numberposts' => -1, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + )); + + $pages = array(); + foreach ( $posts as $post ) { + $pdf_links = simplepdf_extract_pdf_links($post->post_content); + if ( ! empty($pdf_links) ) { + $pages[] = array('post' => $post, 'pdf_links' => $pdf_links); + } + } + + return array( + 'pages' => $pages, + 'is_capped' => count($post_ids) === SIMPLEPDF_PDF_PAGE_LIMIT, + ); } -function simplepdf_settings_page() { +function simplepdf_get_pages_with_pdf_links() { + static $pages_with_pdf_links = null; + if ( $pages_with_pdf_links === null ) { + $pages_with_pdf_links = simplepdf_scan_pages_with_pdf_links(); + } + + return $pages_with_pdf_links; +} + +function simplepdf_runs_on_post($post_id) { + return simplepdf_get_load_scope() === 'everywhere' + || in_array($post_id, simplepdf_get_selected_post_ids(), true); +} + +function simplepdf_get_link_outcome($pdf_link, $runs_on_page, $has_missing_account) { + switch ( $pdf_link['link_type'] ) { + case 'opens_in_simplepdf': + if ( ! $runs_on_page ) { + return array('opens_in' => 'browser', 'reason' => ''); + } + + return array('opens_in' => $has_missing_account ? 'error' : 'simplepdf', 'reason' => ''); + case 'excluded': + return array('opens_in' => 'browser', 'reason' => __('The link has the exclude-simplepdf class', 'simplepdf-embed')); + case 'not_recognised': + return array('opens_in' => 'browser', 'reason' => __('The link does not point to a .pdf file', 'simplepdf-embed')); + default: + return array('opens_in' => 'browser', 'reason' => ''); + } +} + +function simplepdf_get_view_url($post) { + return $post->post_status === 'publish' ? get_permalink($post) : get_preview_post_link($post); +} + +function simplepdf_render_scan_scope_note($is_capped) { ?> -