| 1 |
<?php |
| 2 |
|
| 3 |
// dirname that's compatible with PHP 5.6 |
| 4 |
function wpc_dirname_r($path, $count=1){ |
| 5 |
if ($count > 1){ |
| 6 |
return dirname(wpc_dirname_r($path, --$count)); |
| 7 |
}else{ |
| 8 |
return dirname($path); |
| 9 |
} |
| 10 |
} |
| 11 |
|
| 12 |
function wpc_course_id_to_url($url, $course_id) { |
| 13 |
if(strpos($url, '?')) { |
| 14 |
$url = $url . '&course_id=' . $course_id; |
| 15 |
} else { |
| 16 |
$url = $url . '?course_id=' . $course_id; |
| 17 |
} |
| 18 |
|
| 19 |
return $url; |
| 20 |
} |
| 21 |
|
| 22 |
function wpc_get_unit($str, $default = '%'){ |
| 23 |
if(strpos($str, '%') !== false){ |
| 24 |
return '%'; |
| 25 |
} elseif(strpos($str, 'px') !== false){ |
| 26 |
return 'px'; |
| 27 |
} elseif(strpos($str, 'em') !== false){ |
| 28 |
return 'em'; |
| 29 |
} else { |
| 30 |
return $default; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
function wpc_get_max_slider_value($str, $default = 100){ |
| 35 |
$str = (int) preg_replace("/[^0-9.]/", "", $str); |
| 36 |
if($str >= $default){ |
| 37 |
return $str; |
| 38 |
} else { |
| 39 |
return $default; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
function wpc_esc_unit($str, $default = '%'){ |
| 44 |
$unit = $default; |
| 45 |
if(empty($str) || $str == 0) { |
| 46 |
return $str; |
| 47 |
} |
| 48 |
if(strpos($str, '%') !== false){ |
| 49 |
$unit = '%'; |
| 50 |
} elseif(strpos($str, 'px') !== false){ |
| 51 |
$unit = 'px'; |
| 52 |
} elseif(strpos($str, 'em') !== false){ |
| 53 |
$unit = 'em'; |
| 54 |
} |
| 55 |
$str = (int) preg_replace("/[^0-9.]/", "", $str); |
| 56 |
return $str . $unit; |
| 57 |
} |
| 58 |
|
| 59 |
function wpc_restrict_content($post_id, $content, $post_name){ |
| 60 |
|
| 61 |
$restriction = get_post_meta( $post_id, 'wpc-lesson-restriction', true ); |
| 62 |
$custom_logged_out_message = get_option('wpc_logged_out_message'); |
| 63 |
$login_url = wp_login_url( get_permalink() ); |
| 64 |
$register_url = wp_registration_url(); |
| 65 |
|
| 66 |
$restricted_message = '<p class="wpc-alert-message wpc-content-restricted wpc-free-account-required">'; |
| 67 |
$restricted_message .= !empty($custom_logged_out_message) ? $custom_logged_out_message : '<a href="' . $login_url . '">' . __('Login', 'wp-courses') . ' </a> or <a href="' . $register_url . '">' . __('Register', 'wp-courses') . '</a> to view this ' . $post_name; |
| 68 |
$restricted_message .= '</p>'; |
| 69 |
|
| 70 |
if( $restriction == 'free-account' && !is_user_logged_in() ){ |
| 71 |
return wp_kses($restricted_message, 'post'); |
| 72 |
} else { |
| 73 |
return $content; |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param int $post_id The post ID |
| 80 |
* @return bool Whether or not someone is allowed to view content based on it's restriction and their logged in, membership and purchase status |
| 81 |
*/ |
| 82 |
|
| 83 |
function wpc_is_restricted($post_id){ |
| 84 |
|
| 85 |
$restriction = get_post_meta( $post_id, 'wpc-lesson-restriction', true ); |
| 86 |
$restricted = false; |
| 87 |
|
| 88 |
if($restriction === 'free-account' && !is_user_logged_in() ) { |
| 89 |
$restricted = true; |
| 90 |
} |
| 91 |
|
| 92 |
if($restriction === 'woo-paid' ){ |
| 93 |
|
| 94 |
if(function_exists('wpc_woo_has_bought')) { |
| 95 |
$restricted = wpc_woo_has_bought( $post_id ) === false ? true : false; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
if($restriction === 'membership'){ |
| 101 |
|
| 102 |
if(function_exists('pmpro_hasMembershipLevel')) { |
| 103 |
|
| 104 |
$page_membership_levels = wpc_pmpro_get_page_levels($post_id); |
| 105 |
$has_membership = pmpro_hasMembershipLevel($page_membership_levels); |
| 106 |
|
| 107 |
$restricted = $has_membership === true ? false : true; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
return $restricted; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
function wpc_has_attachments($lesson_id){ |
| 118 |
$attachment1 = get_post_meta($lesson_id, 'wpc-media-sections-1', true); |
| 119 |
$attachment2 = get_post_meta($lesson_id, 'wpc-media-sections-2', true); |
| 120 |
$attachment3 = get_post_meta($lesson_id, 'wpc-media-sections-3', true); |
| 121 |
|
| 122 |
if(wpc_is_restricted($lesson_id) === true){ |
| 123 |
$has_attachments = false; |
| 124 |
} elseif(!empty($attachment1) || !empty($attachment2) || !empty($attachment3)) { |
| 125 |
$has_attachments = true; |
| 126 |
} else { |
| 127 |
$has_attachments = false; |
| 128 |
} |
| 129 |
|
| 130 |
return $has_attachments; |
| 131 |
} |
| 132 |
|
| 133 |
function wpc_count_posts($post_type = 'course', $status = array('publish')){ |
| 134 |
|
| 135 |
$args = array( |
| 136 |
'post_type' => $post_type, |
| 137 |
'post_status' => $status, |
| 138 |
'posts_per_page' => -1, |
| 139 |
'paged' => false, |
| 140 |
); |
| 141 |
|
| 142 |
$query = new WP_Query($args); |
| 143 |
|
| 144 |
return $query->post_count; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param int $results_count The total number of results without pagination |
| 150 |
* @param int $posts_per_page The number of results to display per page |
| 151 |
* @return An array with page numbers |
| 152 |
*/ |
| 153 |
|
| 154 |
function wpc_get_pages($results_count, $posts_per_page){ |
| 155 |
|
| 156 |
if($results_count === 0){ |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
if($posts_per_page > $results_count){ |
| 161 |
return false; |
| 162 |
} else { |
| 163 |
$num_pages = $results_count / $posts_per_page; |
| 164 |
|
| 165 |
$pages = array(); |
| 166 |
|
| 167 |
for($i = 1; $i < $num_pages + 1; $i++) { |
| 168 |
$pages[] = $i; |
| 169 |
} |
| 170 |
|
| 171 |
return $pages; |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Slightly modified WP function that renders the layout config to the block wrapper. |
| 178 |
* Needed for rendering Gutenberg blocks when fetched via AJAX because the_content() and get_the_content() do not return certain block inline styles. |
| 179 |
* |
| 180 |
* @param string $block_content Rendered block content. |
| 181 |
* @param array $block Block object. |
| 182 |
* @return string Filtered block content and styling. |
| 183 |
*/ |
| 184 |
|
| 185 |
function wpc_render_layout_support_flag( $block_content, $block ) { |
| 186 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 187 |
$support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
| 188 |
|
| 189 |
if ( ! $support_layout ) { |
| 190 |
return $block_content; |
| 191 |
} |
| 192 |
|
| 193 |
$block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) ); |
| 194 |
$default_layout = wp_get_global_settings( array( 'layout' ) ); |
| 195 |
$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false; |
| 196 |
$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() ); |
| 197 |
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout; |
| 198 |
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) { |
| 199 |
if ( ! $default_layout ) { |
| 200 |
return $block_content; |
| 201 |
} |
| 202 |
$used_layout = $default_layout; |
| 203 |
} |
| 204 |
|
| 205 |
$class_name = wp_unique_id( 'wp-container-' ); |
| 206 |
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) ); |
| 207 |
// Skip if gap value contains unsupported characters. |
| 208 |
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here |
| 209 |
// because we only want to match against the value, not the CSS attribute. |
| 210 |
if ( is_array( $gap_value ) ) { |
| 211 |
foreach ( $gap_value as $key => $value ) { |
| 212 |
$gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; |
| 213 |
} |
| 214 |
} else { |
| 215 |
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; |
| 216 |
} |
| 217 |
|
| 218 |
$fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' ); |
| 219 |
|
| 220 |
// If a block's block.json skips serialization for spacing or spacing.blockGap, |
| 221 |
// don't apply the user-defined value to the styles. |
| 222 |
$should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' ); |
| 223 |
$style = wp_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value ); |
| 224 |
// This assumes the hook only applies to blocks with a single wrapper. |
| 225 |
// I think this is a reasonable limitation for that particular hook. |
| 226 |
$content = preg_replace( |
| 227 |
'/' . preg_quote( 'class="', '/' ) . '/', |
| 228 |
'class="' . esc_attr( $class_name ) . ' ', |
| 229 |
$block_content, |
| 230 |
1 |
| 231 |
); |
| 232 |
|
| 233 |
return '<style>' . $style . '</style>' . $content; |
| 234 |
} |
| 235 |
|
| 236 |
?> |