/** * Display a link to 'View Full Website' * * Ex: [link_back_to_device link_text="Return to Mobile Website" css_classes="blue-text, alignleft"] * * @param $atts array containing 2 indicies 'link_text' and 'css_classes'. * These determine the output <a> text and the css classes applied to the <a> * @return the generated <a> tag which, when clicked, takes the user back to their respective device */ static function link_back_to_device_shortcode($atts, $content, $name) { extract(shortcode_atts(array('link_text' => __('Return to Mobile Website', 'device-theme-switcher'), 'css_classes' => array()), $atts)); // Generate the html anchor link $html_output = DTS_Switcher::get_instance()->build_html_link('device', $link_text, explode(',', str_replace(' ', '', $css_classes)), false); return $html_output; }
/** * Hook into WordPress * * @return null */ public function hook_into_wordpress() { // Only run the following in the admin, the REAL admin--not admin ajax if (is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX)) { // Grab the single instance of the admin class $dts_admin = DTS_Admin::get_instance(); // Create our plugin admin page under the 'Appearance' menu add_action('admin_menu', array($dts_admin, 'admin_menu'), 10, 0); // Check if we need to save any form data that was submitted add_action('load-appearance_page_device-themes', array($dts_admin, 'load'), 10, 0); // Display a 'Settings' link with the plugin in the plugins list add_filter('plugin_action_links', array($this, 'device_theme_switcher_settings_link'), 10, 2); // Add admin scripts and styles used to display the admin settings view add_action('admin_init', array($this, 'admin_enqueue_scripts')); } else { // is_admin() // Grab the single instance of the switcher class // And make it available globally for use in themes/other plugins global $dts; $dts = DTS_Switcher::get_instance(); // Hook into the template output function with a filter and change the template delivered if need be add_filter('template', array($dts, 'deliver_template'), 10, 0); // Hook into the stylesheet output function with a filter and change the stylesheet delivered if need be add_filter('stylesheet', array($dts, 'deliver_stylesheet'), 10, 0); } // if is_admin() // Load the plugin widgets add_action('widgets_init', array($this, 'register_widgets'), 10, 0); // Add our shortcodes DTS_Shortcode::get_instance()->add_shortcodes(); }
/** * Switch Theme Link Generation * * Generate an html anchor link for the end user to click and 'View Full Website', or to go 'Back to Mobile Site'. * This function is called by the plugins template tags and shortcodes. * * @uses get_option() * @param string $link_type valid values are 'active' or 'device'. E.g. generate a link to * view the active theme, aka 'View Full Website', or a link back to the device i.e. 'Back to Mobile Site' * @param string $link_text The text within the generated link, i.e. <a>View Full Website</a> * @param array $css_classes And array of additional css classes to be applied to the generated link. * Note: the classes to-full-website and back-to-mobile are added by default * @param boolean $echo 1 or 0 or true or false, by default the link is echoed to the screen. False will * return it for use as a string. * @return string HTML Anchor Link */ public static function build_html_link($link_type = 'active', $link_text = "View Full Website", $css_classes = array(), $echo = true) { // Grab the single instance of our dts switcher for use below $dts = DTS_Switcher::get_instance(); // Grab the samed cookie name $cookie_name = get_option('dts_cookie_name'); // Set some defaults $html_output = $target_theme = ""; //The link_type will either be 'active' or 'device' //'active' pertains to the link for devices to 'view the full website'\'active' theme //'device' pertains to the link for devices which viewed the full website to go 'Back to the Mobile Theme' switch ($link_type) { case 'active': //add a css class to help identify the link type for developers to style the output how they please array_unshift($css_classes, 'to-full-website'); //determine if the 'View Full Website' link should be displayed or not if (($dts->device != 'active' || $dts->device == 'active' && !empty($dts->theme_override)) && $dts->theme_override != 'active' && !empty($dts->{$dts->device . "_theme"})) { ## don't show it if the device theme does not exist, and the user is currently viewing the active theme $target_theme = "active"; ## display the link under all other conditions } break; case 'device': //add a css class to help identify the link type for developers to style the output how they please array_unshift($css_classes, 'back-to-mobile'); /** * show the link.. * * when the current device is not 'active' (not really a device) * when there is a cookie set and no GET theme variable * when there is a cookie set and and there is a GET theme variable which does not equal the current device * when there is no cookie set, but there is a GET theme variable which does not equal the current device */ if ($dts->device != 'active') { if (isset($_COOKIE[$cookie_name])) { if (isset($_GET['theme'])) { if ($_GET['theme'] != $dts->device) { $target_theme = $dts->device; } } else { $target_theme = $dts->device; } } else { if (isset($_GET['theme'])) { if ($_GET['theme'] != $dts->device) { $target_theme = $dts->device; } } } } break; } // Only output the html link if the above logic determines if a link should be shown or not if (!empty($target_theme)) { // Start the link as protocolless and containing the current host address $link_href = "//" . $_SERVER['HTTP_HOST']; #ex //local.wordpress.dev or //www.site.com // Next add on everything before a ?, i.e. any pretty url pages $link_href .= strtok($_SERVER['REQUEST_URI'], '?'); #ex //local.wordpress.dev/sample-page/ // Create an array $query_array which contains the current query vars in GET parse_str($_SERVER['QUERY_STRING'], $query_array); #ex: array('page_id' => 2) // Add the Device Theme Switcher 'theme=active' query var to the other existing vars $query_array['theme'] = $target_theme; #ex: array('page_id' => 2, 'theme' => 'active') // Add our assembled query vars onto the end of the link $link_href .= "?" . http_build_query($query_array); #ex: local.wordpress.dev/sample-page/?theme=active // Ensure 'dts-link' is the first css class in the link array_unshift($css_classes, 'dts-link'); // Build the HTML link $html_output = "<a href='{$link_href}' title='{$link_text}' class='" . implode(' ', $css_classes) . "'>{$link_text}</a>\n"; } if ($echo) { // Echo the HTML link echo $html_output; } else { // Return the HTML link return $html_output; } }
// Bail if this file is being accessed directly defined('ABSPATH') or exit; /** * Backwards compatibility for Device Theme Switcher v1.0.0 URL $_GET Variables * * This code only assists users who have hardcoded ?dts_device= links into thier themes * Captures the following $_GET variables and ports them to the new code logic: * * ?dts_device=screen * Previously used allow the handheld/tablet user to view the full website * * ?dts_device=device * previously used to allow the handheld/tablet user return to their device's assigned theme */ if (isset($_GET['dts_device'])) { switch ($_GET['dts_device']) { //'active' now represents the 'full website' //'active' theme as in the active theme set by the admin within wp-admin/themes.php case 'screen': $_GET['theme'] = 'active'; break; case 'device': // Set the get 'theme' variable to match what our newer code is expecting $_GET['theme'] = DTS_Switcher::get_instance()->device; break; } // end switch // This really shouldn't be needed, but just to ensure it's been removed during runtime unset($_GET['dts_device']); } // end if