Source: my-calendar-print.php

  1. <?php
  2. /**
  3. * Output the print view.
  4. *
  5. * @category Calendar
  6. * @package My Calendar
  7. * @author Joe Dolson
  8. * @license GPLv2 or later
  9. * @link https://www.joedolson.com/my-calendar/
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. add_action( 'template_redirect', 'my_calendar_print_view' );
  15. /**
  16. * Redirect to print view if query set.
  17. */
  18. function my_calendar_print_view() {
  19. if ( isset( $_GET['cid'] ) && 'mc-print-view' === $_GET['cid'] ) {
  20. my_calendar_print();
  21. exit;
  22. }
  23. }
  24. /**
  25. * Produce print view output.
  26. */
  27. function my_calendar_print() {
  28. $mc_version = mc_get_version();
  29. $mc_version .= ( SCRIPT_DEBUG ) ? '-' . wp_rand( 10000, 99999 ) : '';
  30. $url = plugin_dir_url( __FILE__ );
  31. // The time string can contain a plus literal, which needs to be re-encoded.
  32. $time = ( isset( $_GET['time'] ) ) ? sanitize_text_field( urlencode( wp_unslash( $_GET['time'] ) ) ) : 'month';
  33. $category = ( isset( $_GET['mcat'] ) ) ? sanitize_text_field( wp_unslash( $_GET['mcat'] ) ) : '';
  34. $ltype = ( isset( $_GET['ltype'] ) ) ? sanitize_text_field( wp_unslash( $_GET['ltype'] ) ) : '';
  35. $lvalue = ( isset( $_GET['lvalue'] ) ) ? sanitize_text_field( wp_unslash( $_GET['lvalue'] ) ) : '';
  36. header( 'Content-Type: ' . get_bloginfo( 'html_type' ) . '; charset=' . get_bloginfo( 'charset' ) );
  37. if ( mc_file_exists( 'mc-print.css' ) ) {
  38. $stylesheet = mc_get_file( 'mc-print.css', 'url' );
  39. } else {
  40. $stylesheet = $url . 'css/mc-print.css';
  41. }
  42. $args = array(
  43. 'type' => 'print',
  44. 'category' => $category,
  45. 'time' => $time,
  46. 'ltype' => $ltype,
  47. 'lvalue' => $lvalue,
  48. );
  49. $return_url = mc_get_uri( false, $args );
  50. /**
  51. * Filter the root URL used to generate the return URL.
  52. *
  53. * @hook mc_print_return_url
  54. *
  55. * @param {string} $return_url Referer URL for calendar print view arrived from.
  56. * @param {string} $category Category argument.
  57. * @param {string} $time Time argument.
  58. * @param {string} $ltype Location type argument.
  59. * @param {string} $lvalue Location value argument.
  60. *
  61. * @return {string}
  62. */
  63. $return_url = apply_filters( 'mc_print_return_url', $return_url, $category, $time, $ltype, $lvalue );
  64. if ( isset( $_GET['href'] ) ) {
  65. // Only support URLs on the same home_url().
  66. $ref_url = sanitize_text_field( urldecode( wp_unslash( $_GET['href'] ) ) );
  67. $ref_root = wp_parse_url( $ref_url )['host'];
  68. $root = wp_parse_url( home_url() )['host'];
  69. $local = ( false !== stripos( $ref_url, home_url() ) && false !== stripos( $root, $ref_root ) ) ? true : false;
  70. if ( $ref_url && $local ) {
  71. $return_url = $ref_url;
  72. } else {
  73. wp_die( 'My Calendar: invalid print URL provided.' );
  74. }
  75. }
  76. ?>
  77. <!DOCTYPE html>
  78. <html <?php language_attributes(); ?>>
  79. <!--<![endif]-->
  80. <head>
  81. <meta charset="<?php echo esc_attr( get_bloginfo( 'charset' ) ); ?>" />
  82. <meta name="viewport" content="width=device-width" />
  83. <title><?php echo esc_html( get_bloginfo( 'name' ) ) . ' - ' . esc_html__( 'Calendar: Print View', 'my-calendar' ); ?></title>
  84. <meta name="generator" content="My Calendar for WordPress" />
  85. <meta name="robots" content="noindex,nofollow" />
  86. <!-- Copy mc-print.css to your theme directory if you wish to replace the default print styles -->
  87. <link rel="stylesheet" href="<?php echo esc_url( includes_url( '/css/dashicons.css' ) ); ?>" type="text/css" media="screen,print" />
  88. <link rel="stylesheet" href="<?php echo esc_url( add_query_arg( 'version', $mc_version, $stylesheet ) ); ?>" type="text/css" media="screen,print" />
  89. <?php
  90. /**
  91. * Execute action in the `head` element of the My Calendar print view, where wp_head() won't be run.
  92. *
  93. * @hook mc_print_view_head
  94. *
  95. * @param {string} $output Potential output for My Calendar; default empty string.
  96. */
  97. do_action( 'mc_print_view_head', '' );
  98. ?>
  99. </head>
  100. <body>
  101. <?php
  102. $calendar = array(
  103. 'name' => 'print',
  104. 'format' => 'calendar',
  105. 'category' => $category,
  106. 'time' => $time,
  107. 'ltype' => $ltype,
  108. 'lvalue' => $lvalue,
  109. 'id' => 'mc-print-view',
  110. 'below' => 'key',
  111. 'above' => 'none',
  112. 'json' => 'false',
  113. );
  114. echo wp_kses_post( my_calendar( $calendar ) );
  115. $add = map_deep( wp_unslash( $_GET ), 'sanitize_text_field' );
  116. unset( $add['cid'] );
  117. unset( $add['feed'] );
  118. unset( $add['href'] );
  119. /**
  120. * Return to calendar URL from print view.
  121. *
  122. * @hook mc_return_to_calendar
  123. *
  124. * @param {string} $return_url URL to return to previous page.
  125. * @param {array} $add Array of parameters added to this URL.
  126. *
  127. * @return {string}
  128. */
  129. $return_url = apply_filters( 'mc_return_to_calendar', mc_build_url( $add, array( 'feed', 'cid', 'href', 'searched' ), $return_url ), $add );
  130. if ( $return_url ) {
  131. echo "<p class='return'><a href='" . esc_url( $return_url ) . "'><span class='dashicons dashicons-arrow-left-alt' aria-hidden='true'></span> " . esc_html__( 'Return to calendar', 'my-calendar' ) . '</a> <a href="javascript:window.print()"><span class="dashicons dashicons-printer" aria-hidden="true"></span> ' . esc_html( __( 'Print', 'my-calendar' ) ) . '</a></p>';
  132. }
  133. ?>
  134. </body>
  135. </html>
  136. <?php
  137. }