learn.php 9.93 KB
<?php 
add_shortcode( 'breadcrumbs', 'the_bread' );


function the_bread() { 
    if(!is_admin()){
        $ingredients = array(
            'separator' => ' | ',
            'offset' => -3,
            'length' => 3,
        );
        ob_start(); 
        $flour = $_SERVER['REQUEST_URI'];

        if ( str_contains( $flour, '?' ) )
            $flour = substr( $flour, 0, strpos( $flour, '?' ) );

        $flour = ( str_ends_with( $flour, '/' ) ? explode( '/', substr( $flour, 1, -1 ) ) : explode( '/', substr( $flour, 1 ) ) );

        $crumbs = [];

        foreach ( $flour as $crumb ) {

            $slug = esc_html( $crumb );

            $url = esc_url( $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . substr( implode( '/', $flour ), 0, strpos( implode( '/', $flour ), $crumb ) ) . $crumb. '/' );

            array_push( $crumbs, ( object )
                [
                    'slug' => $slug,
                    'url' => $url,
                ]
            );

        };

        $offset =  ( empty( $ingredients['offset'] ) ? 0 : $ingredients['offset'] );
        $length =  ( empty( $ingredients['length'] ) ? null : $ingredients['length'] );

        $crumbs = array_slice( $crumbs, $offset, $length );

        echo '<ul class="bread">';

        $i = 0;
        if ( is_singular( 'sfwd-lessons' ) ) {
        echo '<li class="crumb" itemprop="itemListElement">
            <a class="item" itemprop="item" href="https://thecommonwell.ca/learning-2/">
                <span itemprop="name">Learning Opportunities</span>
            </a>
            <meta itemprop="position" content="1">
        </li>&gt;';
        }
        foreach ( $crumbs as $crumb ) {
            $i++;

            echo '<li class="crumb" itemprop="itemListElement">
                <a class="item" itemprop="item" href="' . $crumb->url . '">
                    <span itemprop="name">' . ( url_to_postid( $crumb->url ) ? get_the_title( url_to_postid( $crumb->url ) ) : ucfirst( str_replace( '-', ' ', $crumb->slug ) ) ) . '</span>
                </a>
                <meta itemprop="position" content="' . $i . '">
            </li>';

            if ( $i !== sizeof( $crumbs ) && ! empty( $ingredients['separator'] ) )
                echo $ingredients['separator'];

        };

        echo '</ul>';
        $output = ob_get_clean(); 
        return $output;
    }

};


add_filter( 'woocommerce_account_menu_items', 'misha_rename_downloads' );

function misha_rename_downloads( $menu_links ){
	
	// $menu_links[ 'TAB ID HERE' ] = 'NEW TAB NAME HERE';
	$menu_links[ 'dashboard' ] = 'My Account';
    $menu_links[ 'edit-address' ] = 'Address';

	return $menu_links;
}


add_shortcode( 'certs', 'certs' );

function certs(){

    try{
    $enrolled_courses = learndash_user_get_enrolled_courses( get_current_user_id(), array(), false);
     
      
   ob_start(); 

   if (is_array($enrolled_courses)):?>
   
   <div class="carousel">
        <div id="cert" class="cert-carousel carousel-items container">
            <div class='swiper-wrapper'>
                <?php  foreach($enrolled_courses as $enrolled_course){
                          $cert = learndash_get_course_certificate_link($enrolled_course, get_current_user_id());
                          if(!empty($cert)){
                                $sfwd_course = get_post_meta($enrolled_course , '_sfwd-courses', true );
                                $thumbnail_id = "911"; ?> 
                                <div class="swiper-slide">
                                        <div class="cert"> 
                                            <?php echo '<a target="_blank" style="background-image:url('.wp_get_attachment_url( $thumbnail_id).')" class="cert_link" href="'.$cert.'">'. wp_get_attachment_image($thumbnail_id,'medium' ).'<div class="cert-text"><p><strong>'
                                            .get_user_meta(get_current_user_id(), 'first_name', true )." ".  get_user_meta(get_current_user_id(), 'last_name', true ).'</strong></p>'
                                            .get_the_title( $enrolled_course ).'</div></a>'; 
                                            ?>
                                        </div>
                                </div>
                          <?php  }
                         }; ?> 
            </div>
                <div class="swiper-pagination"></div>
                <div class="swiper-button-prev" data-id="cert"></div>
                <div class="swiper-button-next" data-id="cert"></div>
        </div>        
    </div>  
    
  <?php endif;
    wp_reset_query();
    $output = ob_get_clean();
    return $output;
    }catch(Throwable $e) { 
        error_log("certs()". $e->getMessage()) ;
    }
}

function wooc_extra_register_fields() {?>
    <p class="form-row form-row-first">
    <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
    </p>
    <p class="form-row form-row-last">
    <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
    </p>
    <div class="clear"></div>
    <?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );


function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
           $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
    }
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
           $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
    }
       return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );





// ----- validate password match on the registration page
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
	global $woocommerce;
	extract( $_POST );
	if ( strcmp( $password, $password2 ) !== 0 ) {
		return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
	}
	return $reg_errors;
}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);

// ----- add a confirm password fields match on the registration page
function wc_register_form_password_repeat() {
	?>
	<p class="form-row form-row-wide">
		<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
		<input type="password" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
	</p>
	<?php
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );

// ----- Validate confirm password field match to the checkout page
function lit_woocommerce_confirm_password_validation( $posted ) {
    $checkout = WC()->checkout;
    if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
        if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
            wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' ); 
        }
    }
}
add_action( 'woocommerce_after_checkout_validation', 'lit_woocommerce_confirm_password_validation', 10, 2 );

// ----- Add a confirm password field to the checkout page
function lit_woocommerce_confirm_password_checkout( $checkout ) {
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {

        $fields = $checkout->get_checkout_fields();

        $fields['account']['account_confirm_password'] = array(
            'type'              => 'password',
            'label'             => __( 'Confirm password', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
        );

        $checkout->__set( 'checkout_fields', $fields );
    }
}
add_action( 'woocommerce_checkout_init', 'lit_woocommerce_confirm_password_checkout', 10, 1 );

add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );