| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Template_Loader |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class UpStream_Template_Loader |
| 15 |
*/ |
| 16 |
class UpStream_Template_Loader { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get things going |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
add_filter( 'template_include', array( $this, 'template_loader' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Load a template. |
| 29 |
* |
| 30 |
* Handles template usage so that we can use our own templates instead of the themes. |
| 31 |
* |
| 32 |
* Templates are in the 'templates' folder. upstream looks for theme. |
| 33 |
* overrides in /theme/upstream/ by default. |
| 34 |
* |
| 35 |
* @param mixed $template Template data. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function template_loader( $template ) { |
| 40 |
$get_data = isset( $_GET ) ? wp_unslash( $_GET ) : array(); |
| 41 |
$server_data = isset( $_SERVER ) ? wp_unslash( $_SERVER ) : array(); |
| 42 |
$file = ''; |
| 43 |
|
| 44 |
if ( get_post_type() === false ) { |
| 45 |
if ( ! upstream_is_project_base_uri( sanitize_text_field( $server_data['REQUEST_URI'] ) ) ) { |
| 46 |
return $template; |
| 47 |
} |
| 48 |
} elseif ( 'project' !== get_post_type() ) { |
| 49 |
return $template; |
| 50 |
} |
| 51 |
|
| 52 |
if ( is_single() ) { |
| 53 |
require_once UPSTREAM_PLUGIN_DIR . 'includes/admin/metaboxes/metabox-functions.php'; |
| 54 |
|
| 55 |
$file = 'single-project.php'; |
| 56 |
|
| 57 |
$user_id = get_current_user_id(); |
| 58 |
$project_id = upstream_post_id(); |
| 59 |
|
| 60 |
if ( ! upstream_user_can_access_project( $user_id, $project_id ) ) { |
| 61 |
wp_redirect( get_post_type_archive_link( 'project' ) ); |
| 62 |
exit; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if ( is_archive() ) { |
| 67 |
$file = 'archive-project.php'; |
| 68 |
} |
| 69 |
|
| 70 |
if ( isset( $get_data['action'] ) && sanitize_text_field( $get_data['action'] ) === 'logout' ) { |
| 71 |
UpStream_Login::do_destroy_session(); |
| 72 |
$url_base = upstream_get_project_base(); |
| 73 |
|
| 74 |
if ( preg_match( '/^\/' . $url_base . '/i', sanitize_text_field( $server_data['REQUEST_URI'] ) ) ) { |
| 75 |
$redirect_to = wp_login_url( get_post_type_archive_link( 'project' ) ); |
| 76 |
} else { |
| 77 |
$redirect_to = get_permalink(); |
| 78 |
} |
| 79 |
|
| 80 |
wp_save_redirect( $redirect_to ); |
| 81 |
exit; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* Login page if not logged in |
| 86 |
*/ |
| 87 |
if ( ! apply_filters( 'upstream_permissions_filter_page_access', false ) && ! upstream_is_user_logged_in() ) { |
| 88 |
$file = 'login.php'; |
| 89 |
$GLOBALS['login_template'] = true; |
| 90 |
} |
| 91 |
|
| 92 |
if ( $file ) { |
| 93 |
$check_dirs = array( |
| 94 |
trailingslashit( get_stylesheet_directory() ) . upstream_template_path(), |
| 95 |
trailingslashit( get_template_directory() ) . upstream_template_path(), |
| 96 |
UPSTREAM_PLUGIN_DIR . 'templates/', |
| 97 |
); |
| 98 |
|
| 99 |
foreach ( $check_dirs as $dir ) { |
| 100 |
if ( file_exists( trailingslashit( $dir ) . $file ) ) { |
| 101 |
load_template( $dir . $file ); |
| 102 |
|
| 103 |
return; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $template; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
new UpStream_Template_Loader(); |
| 113 |
|