Last active
August 10, 2020 12:47
-
-
Save venturaEffect/daca393c8baff83c96dbc40b8df7e83f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Astra Child Theme functions and definitions | |
| * | |
| * @link https://developer.wordpress.org/themes/basics/theme-functions/ | |
| * | |
| * @package Astra Child | |
| * @since 1.0.0 | |
| */ | |
| /** | |
| * Define Constants | |
| */ | |
| define( 'CHILD_THEME_ASTRA_CHILD_VERSION', '1.0.0' ); | |
| /** | |
| * Enqueue styles | |
| */ | |
| function child_enqueue_styles() { | |
| wp_enqueue_style( 'astra-child-theme-css', get_stylesheet_directory_uri() . '/style.css', array('astra-theme-css'), CHILD_THEME_ASTRA_CHILD_VERSION, 'all' ); | |
| } | |
| add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 ); | |
| /* PARA CUSTOMIZAR HEADER */ | |
| function secondary_menus () { | |
| global $current_user; | |
| $c_user_id = get_current_user_id(); | |
| $channel_name = get_user_meta( $c_user_id, 'channel_name', true); | |
| if (!is_user_logged_in() ) { ?> | |
| <div class="btn-ch"> | |
| <a href="https://channels.example.com/My-channel-info/" class="btn-my-ch">my channel info</button> | |
| <a href="https://channels.example.com/My-channel-info/" class="btn-my-ch2">dashboard</a> | |
| </div> | |
| <?php | |
| } else { | |
| if (empty($channel_name)) { ?> | |
| <div class="btn-ch"> | |
| <a href="https://channels.example.com/My-channel-info/" class="btn-my-ch">my channel info</a> | |
| <div class="popup btn-my-ch" onclick="myFunction()">dashboard | |
| <span class="popuptext" id="myPopup">First, <a href="https://channels.example.com/My-channel-info/">create channel</a></span> | |
| </div> | |
| </div> | |
| <script> | |
| // When the user clicks on <div>, open the popup | |
| function myFunction() { | |
| var popup = document.getElementById("myPopup"); | |
| popup.classList.toggle("show"); | |
| } | |
| </script> | |
| <?php | |
| } else { ?> | |
| <div class="btn-ch"> | |
| <a href="https://channels.example.com/My-channel-info/" class="btn-my-ch">my channel info</a> | |
| <a class="btn-my-ch2" target="pupu" onclick="window.open('https://channels.example.com/wp-admin/admin.php?page=ir_instructor_overview','pupu','width=950,height=600');return false;">dashboard</a> | |
| </div> | |
| <?php | |
| } | |
| } | |
| } | |
| add_action( 'wp_head', 'secondary_menus', 10); | |
| /* mY CHANNEL INFO FORM UPDATE */ | |
| function form_update() { | |
| global $post; | |
| if( $post->ID == 153) { | |
| /* Get user info. */ | |
| global $current_user, $wp_roles; | |
| /* Load the registration file. */ | |
| $error = array(); | |
| /* If profile was saved, update profile. */ | |
| if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) { | |
| /* Update user password. */ | |
| if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) { | |
| if ( $_POST['pass1'] == $_POST['pass2'] ) | |
| wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) ); | |
| else | |
| $error[] = __('The passwords you entered do not match. Your password was not updated.', 'profile'); | |
| } | |
| /* Update user information. */ | |
| if ( !empty( $_POST['url'] ) ) | |
| wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_POST['url'] ) ) ); | |
| if ( !empty( $_POST['email'] ) ){ | |
| if (!is_email(esc_attr( $_POST['email'] ))) | |
| $error[] = __('The Email you entered is not valid. please try again.', 'profile'); | |
| elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id ) | |
| $error[] = __('This email is already used by another user. try a different one.', 'profile'); | |
| else{ | |
| wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] ))); | |
| } | |
| } | |
| if ( !empty( $_POST['first-name'] ) ) | |
| update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) ); | |
| if ( !empty( $_POST['last-name'] ) ) | |
| update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) ); | |
| if ( !empty( $_POST['description'] ) ) | |
| update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) ); | |
| if ( !empty( $_POST['channel_name'] ) ) | |
| if(channel_exists( esc_attr( $_POST['channel_name'] )) != $current_user->id ) | |
| $error[] = __('This channel is already used by another user. try a different one.', 'profile'); | |
| else { | |
| update_user_meta( $current_user->ID, 'channel_name', esc_attr( $_POST['channel_name'] ) ); | |
| } | |
| if ( !empty( $_POST['pic'] ) ) | |
| update_user_meta( $current_user->ID, 'pic', esc_attr( $_POST['pic'] ) ); | |
| if ( !empty( $_POST['description_channel'] ) ) | |
| update_user_meta( $current_user->ID, 'description_channel', esc_attr( $_POST['description_channel'] ) ); | |
| /* Redirect so the page will show updated info.*/ | |
| /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */ | |
| if ( !$error ) { | |
| wp_redirect( get_permalink() .'?updated=true' ); | |
| exit; | |
| } | |
| } | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; // Exit if accessed directly. | |
| } | |
| } | |
| } | |
| add_action( 'wp_enqueue_scripts', 'form_update' ); | |
| function custom_post () { | |
| global $post; | |
| if( $post->ID == 153) { | |
| /* Get user info. */ | |
| global $current_user, $wp_roles; | |
| /* Load the registration file. */ | |
| $error = array(); | |
| /* If profile was saved, update profile. */ | |
| if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) { | |
| var_dump($_POST); | |
| if ( !empty( $_POST['channel_name']) && !empty( $_POST['description_channel']) && !empty( $_POST['pic'] ) ) { | |
| // Finally, save/update the channel! | |
| $user = get_user_by( 'ID', $user_id ); | |
| $channel_id = get_channel_by_author( $user_id ); | |
| $args = [ | |
| 'ID' => $channel_id, | |
| 'post_title' => $_POST['channel_name'], | |
| 'post_name' => $_POST['channel_name'], // set the slug/permalink | |
| 'post_content' => $_POST['description_channel'], | |
| 'post_status' => 'publish', | |
| 'post_type' => 'channel', | |
| ]; | |
| $post_id = wp_insert_post( $args, true ); | |
| var_dump($_POST); | |
| // check if we were succesful. | |
| if ( is_wp_error( $post_id ) ) { | |
| // it didn't work! | |
| error_log( $post_id->get_error_message() ); | |
| } else if ( empty( $post_id ) ) { | |
| // it didn't work! | |
| error_log( "post_id is empty/false" ); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| add_action( 'wp', 'custom_post' ); | |
| /* CREATING CHANNEL PAGES FROM USER PROFILES | |
| function create_channel_page( $user_id = '' ) { | |
| $user = new WP_User($user_id); | |
| if ( ! $user->ID ) return ''; | |
| // check if the user whose profile is updating has already a post | |
| global $wpdb; | |
| $channel_post_exists = $wpdb->get_var( $wpdb->prepare( | |
| "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = 'channel' and post_status = 'publish'", $user->channel_name | |
| ) ); | |
| // you have a custom role for channels | |
| // you should, because if not all user will have a page, also admin, subscribers... | |
| // if ( ! in_array('instructor', $user->roles) ) return ''; | |
| $user_info = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user->ID ) ); | |
| $title = $user_info['channel_name']; | |
| // of course create the content as you want | |
| $content = 'This is the page for: '; | |
| $content .= $user_info['description_channel']; | |
| $post = array( | |
| 'post_title' => $title, | |
| 'post_name' => $user->channel_name, | |
| 'post_content' => $content, | |
| 'post_status' => 'publish', | |
| 'post_type' => 'channel' | |
| ); | |
| if ( $channel_post_exists ) { | |
| $post['ID'] = $channel_post_exists; | |
| wp_update_post( $post ); | |
| } else { | |
| wp_insert_post( $post ); | |
| } | |
| } | |
| add_action( 'personal_options_update', 'create_channel_page' ); | |
| add_action( 'edit_user_profile_update', 'create_channel_page' ); | |
| /* REMOVE OLD CHANNELS WHEN UPDATE | |
| add_action( 'save_post', 'removing_older_posts' ); | |
| function removing_older_posts() { | |
| $args = array ( | |
| 'post_type' => 'channel', | |
| 'author' => get_user_meta($user->ID), | |
| 'orderby' => 'date', | |
| ); | |
| $query = new WP_Query( $args ); | |
| if ( $query->have_posts() ) { //if the current user has posts under his/her name | |
| $i = 0; //create post counter | |
| while ( $query->have_posts() ) { //loop through each post | |
| $query->the_post(); //get the current post | |
| if ($i > 0) { //if you're not on the first post | |
| wp_delete_post( $query->post->ID, true ); //delete the post | |
| } | |
| $i++; //increment the post counter | |
| } | |
| wp_reset_postdata(); | |
| } | |
| } | |
| */ | |
| function get_channel_by_author( $user_id ) : int { | |
| global $current_user, $wp_roles; | |
| $user_id = get_current_user_id(); | |
| // if we don't find anything then a value of 0 will create a new post rather than update an existing post. | |
| $channel_id = 0; | |
| $args = [ | |
| 'post_type' => 'channel', | |
| 'posts_per_page' => 1, | |
| 'post_author' => $user_id, | |
| ]; | |
| $query = new WP_Query( $args ); | |
| if ( $query->have_posts() ) { | |
| while ( $query->have_posts() ) { | |
| $query->the_post(); | |
| $channel_id = get_the_ID(); | |
| // we only want a single post ID | |
| break; | |
| } | |
| // cleanup after the query | |
| wp_reset_postdata(); | |
| } else { | |
| // perhaps the channel was created before post authors were set? Fall back to looking for the post by its slug/name | |
| return 0; | |
| } | |
| return $channel_id; | |
| } | |
| function get_channel_by_slug( string $slug ) : int { | |
| $channel_id = 0; | |
| $args = [ | |
| 'post_type' => 'channel', | |
| 'posts_per_page' => 1, | |
| 'post_name' => $slug | |
| ]; | |
| //... etc etc see get_channel_by_author ... | |
| return $channel_id; | |
| error_log( $channel_id->get_error_message() ); | |
| } | |
| function handle_channel_post( $user_id = '' ) { | |
| global $current_user, $wp_roles; | |
| $user_id = get_current_user_id(); | |
| /* | |
| // First, is this user an instructor? | |
| $user = get_user_by( 'ID', $user_id ); | |
| if ( ! $user || ! in_array( 'instructor', $user->roles, true ) ) { | |
| return; | |
| } | |
| */ | |
| // get the channel name | |
| $channel_name = get_user_meta( $user_id, 'channel_name', true ); | |
| if ( empty( $channel_name )) { | |
| error_log( "the user does not have a channel_name!" ); | |
| } | |
| // Then lets find this users channel if it exists. | |
| // if we don't find anything then a value of 0 will create a new post rather than update an existing post. | |
| $channel_id = get_channel_by_author( $user_id ); | |
| if ( $channel_id === 0 ) { | |
| // we didn't find a channel with that author, try the channel name. | |
| $channel_id = get_channel_by_slug( $channel_name ); | |
| } | |
| // Finally, save/update the channel! | |
| $content .= get_user_meta( $user_id, 'description_channel', true ); | |
| $args = [ | |
| 'ID' => $channel_id, | |
| 'post_title' => $channel_name, | |
| 'post_name' => $channel_name, // set the slug/permalink | |
| 'post_content' => $content, | |
| 'post_status' => 'publish', | |
| 'post_type' => 'channel', | |
| ]; | |
| $post_id = wp_insert_post( $args ); | |
| // check if we were succesful. | |
| if ( is_wp_error( $post_id ) ) { | |
| // it didn't work! | |
| error_log( $post_id->get_error_message() ); | |
| } else if ( empty( $post_id ) ) { | |
| // it didn't work! | |
| error_log( "post_id is empty/false" ); | |
| } | |
| } | |
| add_action( 'personal_options_update', 'handle_channel_post' ); | |
| add_action( 'edit_user_profile_update', 'handle_channel_post' ); | |
| /* WISDMLABAS SNIPPETS */ | |
| function ir_custom_profile_text( $translation, $text, $domain ) { | |
| // Check if instructor Profile menu | |
| if ( 'wdm_instructor_role' == $domain && 'Profile'== $text ) { | |
| $translation = 'My channel info'; | |
| } | |
| return $translation; | |
| } | |
| add_filter ('gettext', 'ir_custom_profile_text', 10, 3); | |
| // Remove WordPress Logo from the header for instructors | |
| function ir_remove_admin_bar_logo() { | |
| global $wp_admin_bar; | |
| // Check if instructor dashboard | |
| if (! wdm_is_instructor()) { | |
| return; | |
| } | |
| // Remove Logo | |
| $wp_admin_bar->remove_menu( 'wp-logo' ); | |
| } | |
| add_action( 'wp_before_admin_bar_render', 'ir_remove_admin_bar_logo', 11 ); | |
| // To remove the thank you text | |
| add_filter('admin_footer_text', '__return_empty_string', 11); | |
| // To remove the WP version info | |
| add_filter('update_footer', '__return_empty_string', 11); | |
| //------ ESTO ES PARA REDIRECCIONAR AUTHORS PERMALINKS A CUSTOM PAGE ----// | |
| /* | |
| add_filter( 'author_link', 'new_author_link', 10, 1 ); | |
| $users = get_users( array( 'fields' => array( 'ID' ) ) ); | |
| foreach($users as $user){ | |
| $channel_name = get_user_meta($user->ID, 'channel_name', true); | |
| $new_channel_name = str_replace('-', '%20', $channel_name); | |
| } | |
| function new_author_link( $link ) { | |
| $link = 'https://channels.example.com/channels/'.$new_channel_name; //set this however you wish | |
| return $link; //after you've set $link, return it to the filter | |
| } | |
| add_action('wp', function(){ | |
| $users = get_users( array( 'fields' => array( 'ID' ) ) ); | |
| foreach($users as $user){ | |
| $channel_name = get_user_meta($user->ID, 'channel_name', true); | |
| $new_channel_name = str_replace('-', '%20', $channel_name); | |
| } | |
| list($uri, $qs) = explode('?', $_SERVER['REQUEST_URI']); | |
| if ( $uri == "/channels/".$new_channel_name ) /* Change this to path that you want to match*/ /* { | |
| locate_template( "single-channel.php" , true, false ); /* Don't forget to replace template name with actual template which you want to load */ | |
| /* die(); */ /* So that WordPress does not load its template as per template hierarchy. */ | |
| /* } | |
| }); | |
| */ | |
| // Limit media library access | |
| add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' ); | |
| function wpb_show_current_user_attachments( $query ) { | |
| $user_id = get_current_user_id(); | |
| if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts | |
| ') ) { | |
| $query['author'] = $user_id; | |
| } | |
| return $query; | |
| } | |
| ?> |
Again, line #167, $user_id is never defined! You cannot use this variable like that. Use global $user and then $user->ID if you need to get the current user.
Your add_action call is inside the custom_post function. https://gist.github.com/venturaEffect/daca393c8baff83c96dbc40b8df7e83f#file-functions-php-L192 This line can never be reached. You must add the init hook outside of the function declaration (like 2 lines below after the closing brace).
Sorry Tim. Didn't get notified about these messages. Now I've updated and tested as you said.
global $current_user, $wp_roles; $user_id = get_current_user_id();
on all functions where they look for $user_id
Still nothing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/venturaEffect/daca393c8baff83c96dbc40b8df7e83f#file-functions-php-L165 the $user_id variable is never instanciated and $user is never used anyway. This means that you probably always get
0as a result of theget_channel_by_authorfunction on line 166.If you want to check if the result is an error on line 178, you need to set the second parameter of
wp_insert_post($args, true);totrueon the line 175. Else it will returnfalseif there is an error.