Advertisement
Guest User

Kaya QR Code Generator fix

a guest
Mar 4th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.76 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Kaya QR Code Generator - Main Functions.
  4.  * Functions for displaying QR Code image.
  5.  */
  6.  
  7. if (!defined('ABSPATH'))
  8. {
  9.     exit; // Exit if accessed directly
  10. }
  11.  
  12. /**
  13.  * Global boolean $wpkqcg_qrcode_isDisplayed, true if a qrcode is about to be displayed.
  14.  * Used to a better management and lighter loading of scripts and resources.
  15.  *
  16.  * @since 1.1.1
  17.  */
  18. global $wpkqcg_qrcode_isDisplayed;
  19.  
  20. /**
  21.  * Displays QR Code structure.
  22.  *
  23.  * @param array $p_qrcodeValues QR Code form fields values.
  24.  * @param array $p_widgetArgs Arguments from the widget.
  25.  *
  26.  * @return string
  27.  */
  28. if (!function_exists('wpkqcg_doDisplayQRCode'))
  29. {
  30.     function wpkqcg_doDisplayQRCode($p_qrcodeValues, $p_widgetArgs = array())
  31.     {
  32.         global $wpkqcg_qrcode_isDisplayed;
  33.         $wpkqcg_qrcode_isDisplayed = true;
  34.        
  35.         // get QR Code values
  36.         foreach ($p_qrcodeValues as $i_attr => $i_val)
  37.         {
  38.             ${'qrcodeMeta_' . $i_attr} = $i_val;
  39.         }
  40.        
  41.         // set QR Code img ID
  42.         $qrcodeUniqueID = rand(0, 99) . uniqid() . rand(0, 99);
  43.         $qrcodeImgID    = esc_attr('wpkqcg_qrcode_outputimg_' . $qrcodeUniqueID);
  44.        
  45.         // prepare QR Code values
  46.         $qrcodeTitle        = (!empty($qrcodeMeta_title)) ? esc_html($qrcodeMeta_title) : '';
  47.         $qrcodeTitleAlign   = (!empty($qrcodeMeta_title_align)) ? esc_attr($qrcodeMeta_title_align) : '';
  48.         $qrcodeContent      = (!empty($qrcodeMeta_content)) ? esc_attr($qrcodeMeta_content) : '';
  49.         $qrcodeAnchor       = (!empty($qrcodeMeta_anchor)) ? esc_attr($qrcodeMeta_anchor) : '';
  50.         $qrcodeEccLevel     = (!empty($qrcodeMeta_ecclevel)) ? esc_attr($qrcodeMeta_ecclevel) : '';
  51.         $qrcodeNewWindow    = (!empty($qrcodeMeta_new_window)) ? esc_attr($qrcodeMeta_new_window) : '';
  52.         $qrcodeContentURL   = (!empty($qrcodeMeta_content_url)) ? esc_attr($qrcodeMeta_content_url) : '';
  53.         $qrcodeCssShadow    = (!empty($qrcodeMeta_css_shadow)) ? esc_attr($qrcodeMeta_css_shadow) : '';
  54.         $qrcodeAlign        = (!empty($qrcodeMeta_align)) ? esc_attr($qrcodeMeta_align) : '';
  55.         $qrcodeSize         = (!empty($qrcodeMeta_size)) ? esc_attr($qrcodeMeta_size) : '';
  56.         $qrcodeColor        = (!empty($qrcodeMeta_color)) ? esc_attr($qrcodeMeta_color) : '';
  57.         $qrcodeBgColor      = (!empty($qrcodeMeta_bgcolor)) ? esc_attr($qrcodeMeta_bgcolor) : '';
  58.         $qrcodeAlt          = (!empty($qrcodeMeta_alt)) ? esc_attr($qrcodeMeta_alt) : 'QR Code';
  59.        
  60.         // set content as current url
  61.         $qrcodeContentAsCurrentURL = false;
  62.         if (empty($qrcodeContent))
  63.         {
  64.             $qrcodeContent = esc_attr(esc_url((isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"));
  65.             $qrcodeContent .= (!empty($qrcodeAnchor)) ? esc_attr('#' . $qrcodeAnchor) : '';
  66.             $qrcodeContentAsCurrentURL = true;
  67.         }
  68.        
  69.         // set QR Code URL
  70.         if (!empty($qrcodeMeta_url) && strpos($qrcodeMeta_url, 'bitcoin:') === 0) // allow "bitcoin:" as Bitcoin URI scheme
  71.         {
  72.             $qrcodeURL = (!empty($qrcodeMeta_url)) ? esc_attr($qrcodeMeta_url) : '';
  73.         }
  74.         elseif (!empty($qrcodeMeta_url)) // escape regular URL
  75.         {
  76.             $qrcodeURL = (!empty($qrcodeMeta_url)) ? esc_attr(esc_url($qrcodeMeta_url)) : '';
  77.         }
  78.         elseif ($qrcodeContentAsCurrentURL && empty($qrcodeMeta_url) && !empty($qrcodeContentURL)) // take current url
  79.         {
  80.             $qrcodeURL = (!empty($qrcodeContent)) ? esc_attr(esc_url($qrcodeContent)) : '';
  81.         }
  82.        
  83.         // set the title alignment
  84.         $qrcodeTitleInlineAlign = '';
  85.         if ($qrcodeTitleAlign == 'alignleft')
  86.         {
  87.             $qrcodeTitleInlineAlign = 'text-align: left;';
  88.         }
  89.         else if ($qrcodeTitleAlign == 'alignright')
  90.         {
  91.             $qrcodeTitleInlineAlign = 'text-align: right;';
  92.         }
  93.         else if ($qrcodeTitleAlign == 'aligncenter')
  94.         {
  95.             $qrcodeTitleInlineAlign = 'text-align: center;';
  96.         }
  97.        
  98.         // set QR Code image style
  99.         $qrcodeCssInlineBasic = 'width: auto; height: auto; max-width: 100%;';
  100.         $qrcodeCssInlineShadow = (!empty($qrcodeCssShadow)) ? ' box-shadow: 2px 2px 10px #4A4242;' : '';
  101.        
  102.         // set QR Code image alignment
  103.         $qrcodeCssInlineAlign = '';
  104.         $qrcodeLinkCssInlineAlign = 'display: table; width: auto; height: auto; max-width: 100%;';
  105.         $qrcodeClearBlock = '<div style="clear: none;"></div>';
  106.         if ($qrcodeAlign == 'alignleft')
  107.         {
  108.             $qrcodeCssInlineAlign       = ' display: block; float: left; margin-right: 1.5em;';
  109.             $qrcodeLinkCssInlineAlign   = 'display: block; float: left; width: auto; height: auto; max-width: 100%;';
  110.             $qrcodeClearBlock           = '<div style="clear: left;"></div>';
  111.         }
  112.         else if ($qrcodeAlign == 'alignright')
  113.         {
  114.             $qrcodeCssInlineAlign       = ' display: block; float: right; margin-left: 1.5em;';
  115.             $qrcodeLinkCssInlineAlign   = 'display: block; float: right; width: auto; height: auto; max-width: 100%;';
  116.             $qrcodeClearBlock           = '<div style="clear: right;"></div>';
  117.         }
  118.         else if ($qrcodeAlign == 'aligncenter')
  119.         {
  120.             $qrcodeCssInlineAlign       = ' clear: both; display: block; margin-left: auto; margin-right: auto;';
  121.             $qrcodeLinkCssInlineAlign   = 'display: table; width: auto; height: auto; max-width: 100%; margin-left: auto; margin-right: auto;';
  122.             $qrcodeClearBlock           = '<div style="clear: both;"></div>';
  123.         }
  124.        
  125.         // QR Code structure to display
  126.         $output = '<!-- START Kaya QR Code Generator -->';
  127.         $output .= '<div class="wpkqcg_qrcode_wrapper">';
  128.         $output .= '<input type="hidden" id="' . $qrcodeImgID . '_ecclevel" value="' . $qrcodeEccLevel . '" />';
  129.         $output .= '<input type="hidden" id="' . $qrcodeImgID . '_size" value="' . $qrcodeSize . '" />';
  130.         $output .= '<input type="hidden" id="' . $qrcodeImgID . '_color" value="' . $qrcodeColor . '" />';
  131.         $output .= '<input type="hidden" id="' . $qrcodeImgID . '_bgcolor" value="' . $qrcodeBgColor . '" />';
  132.         $output .= '<input type="hidden" id="' . $qrcodeImgID . '_content" value="' . $qrcodeContent . '" />';
  133.        
  134.         // set the title
  135.         if (!empty($qrcodeTitle) && empty($p_widgetArgs))
  136.         {
  137.             $output .= '<h2 style="' . $qrcodeTitleInlineAlign . '">' . $qrcodeTitle . '</h2>'; // shortcode title
  138.         }
  139.         elseif (!empty($qrcodeTitle) && !empty($p_widgetArgs) && !empty($p_widgetArgs['before_title']) && !empty($p_widgetArgs['after_title']))
  140.         {
  141.             if (strpos($p_widgetArgs['before_title'], '>') !== false)
  142.             {
  143.                 $p_widgetArgs['before_title'] = str_replace('>', ' style="' . $qrcodeTitleInlineAlign . '">', $p_widgetArgs['before_title']);
  144.             }
  145.             $output .= $p_widgetArgs['before_title'] . $qrcodeTitle . $p_widgetArgs['after_title']; // widget title
  146.         }
  147.        
  148.         // surround with a link to the URL
  149.         if (!empty($qrcodeURL))
  150.         {
  151.             $output .= '<a href="' . $qrcodeURL . '"';
  152.             $output .= ' style="' . $qrcodeLinkCssInlineAlign . '" ';
  153.             if (!empty($qrcodeNewWindow))
  154.             {
  155.                 $output .= ' target="_blank" rel="noopener noreferrer"'; // open in new window, rel="noopener noreferrer" improves security.
  156.             }
  157.             $output .= '>';
  158.         }
  159.        
  160.         // set QR Code image structure
  161.         $output .= '<img src="" id="' . $qrcodeImgID . '" alt="' . $qrcodeAlt . '" class="wpkqcg_qrcode"';
  162.         $output .= ' style="' . $qrcodeCssInlineBasic . $qrcodeCssInlineShadow . $qrcodeCssInlineAlign . '" >';
  163.        
  164.         // close the link
  165.         if (!empty($qrcodeURL))
  166.         {
  167.             $output .= '</a>';
  168.         }
  169.        
  170.         $output .= $qrcodeClearBlock;
  171.         $output .= '</div>';
  172.         $output .= '<!-- END Kaya QR Code Generator -->';
  173.        
  174.         return $output;
  175.     }
  176. }
  177.  
  178. /**
  179.  * Displays Public scripts in footer.
  180.  * Required for qrcode generation and display.
  181.  *
  182.  * @since 1.1.1
  183.  */
  184. if (!function_exists('wpkqcg_displayInlineScripts'))
  185. {
  186.     function wpkqcg_displayInlineScripts()
  187.     {
  188.         global $wpkqcg_qrcode_isDisplayed;
  189.         if ($wpkqcg_qrcode_isDisplayed)
  190.         {
  191.             $output = '<script type="text/javascript" src="' . WPKQCG_PLUGIN_URL . 'assets/qrcode-v2.min.js?ver=' . WPKQCG_VERSION . '"></script>';
  192.             $output .= '<script type="text/javascript" src="' . WPKQCG_PLUGIN_URL . 'js/wpkqcg-pkg.min.js?ver=' . WPKQCG_VERSION . '"></script>';
  193.             $output .= '<script type="text/javascript">
  194.                            var wp_kqrcg_js_displayed = false;
  195.                            window.addEventListener("DOMContentLoaded", (event) => { wpkqcg_qrcode_display(); wp_kqrcg_js_displayed = true; });
  196.                            jQuery(document).ready(function(){ if(!wp_kqrcg_js_displayed){wpkqcg_qrcode_display();} });
  197.                        </script>';
  198.            
  199.             echo $output;
  200.         }
  201.     }
  202.     add_action('wp_footer', 'wpkqcg_displayInlineScripts');
  203. }
  204.  
  205. /**
  206.  * Enqueue Admin scripts.
  207.  * Required for the shortcode generator assistant, available on pages, posts and custom post types.
  208.  */
  209. if (!function_exists('wpkqcg_enqueueAdminScripts'))
  210. {
  211.     function wpkqcg_enqueueAdminScripts()
  212.     {
  213.         $currentScreen = get_current_screen();
  214.         $currentScreenID = $currentScreen ? $currentScreen->id : '';
  215.         // Get all public post types
  216.         $postTypes = wpkqcg_getAllPostTypesAsList();
  217.            
  218.         if (is_admin() && in_array($currentScreenID, $postTypes))
  219.         {
  220.             wp_enqueue_script('wpkqcg-admin-pkg', WPKQCG_PLUGIN_URL . 'js/wpkqcg-admin-pkg.min.js', array(), WPKQCG_VERSION);
  221.         }
  222.     }
  223.     add_action('admin_enqueue_scripts', 'wpkqcg_enqueueAdminScripts');
  224. }
  225.  
  226. /**
  227.  * Displays Admin scripts in footer.
  228.  * Required for qrcode preview on the shortcode generator assistant.
  229.  *
  230.  * @since 1.2.0
  231.  */
  232. if (!function_exists('wpkqcg_displayInlineAdminScripts'))
  233. {
  234.     function wpkqcg_displayInlineAdminScripts()
  235.     {
  236.         global $wpkqcg_qrcode_isDisplayed;
  237.         if ($wpkqcg_qrcode_isDisplayed)
  238.         {
  239.             $output = '<script type="text/javascript" src="' . WPKQCG_PLUGIN_URL . 'assets/qrcode-v2.min.js?ver=' . WPKQCG_VERSION . '"></script>';
  240.             $output .= '<script type="text/javascript" src="' . WPKQCG_PLUGIN_URL . 'js/wpkqcg-pkg.min.js?ver=' . WPKQCG_VERSION . '"></script>';
  241.             $output .= '<script type="text/javascript">window.addEventListener("DOMContentLoaded", (event) => {wpkqcg_qrcode_preview_display();});</script>';
  242.            
  243.             echo $output;
  244.         }
  245.     }
  246.     add_action('admin_footer', 'wpkqcg_displayInlineAdminScripts');
  247. }
  248.  
  249. /**
  250.  * Get all public post types.
  251.  * Return public basics post types and custom post types as list.
  252.  *
  253.  * @return array
  254.  *
  255.  * @since 1.3.0
  256.  */
  257. if (!function_exists('wpkqcg_getAllPostTypesAsList'))
  258. {
  259.     function wpkqcg_getAllPostTypesAsList()
  260.     {
  261.         // Get all post types as list
  262.         $postTypesArgs = array(
  263.            'public' => true,
  264.         );
  265.         $postTypesOutput    = 'names';
  266.         $postTypesOperator  = 'and';
  267.         $postTypes          = get_post_types($postTypesArgs, $postTypesOutput, $postTypesOperator);
  268.        
  269.         // Remove some built ins or others
  270.         $postTypesRemove    = array('attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block');
  271.         $postTypesCleaned   = array();
  272.        
  273.         foreach ($postTypes as $i_postType)
  274.         {
  275.             if (in_array($i_postType, $postTypesRemove)) continue;
  276.             $postTypesCleaned[esc_attr($i_postType)] = esc_attr($i_postType);
  277.         }
  278.        
  279.         // Add admin options page
  280.         $postTypesCleaned['wpkqcg_admin_dashboard'] = esc_attr(get_plugin_page_hookname(WPKQCG_Admin_Dashboard::$_pageSlug, WP_KayaStudio_Plugins_Admin_Dashboard::$_pageSlug));
  281.        
  282.         return $postTypesCleaned;
  283.     }
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement