' . all_in_one_invite_codes_md5( $post->ID ) . '', 'all_in_one_invite_codes_render_metabox', 'tk_invite_codes', 'normal', 'default' ); } add_action( 'add_meta_boxes_tk_invite_codes', 'all_in_one_invite_codes_create_metabox' ); /** * Render the metabox and display the options * * @param $post */ function all_in_one_invite_codes_render_metabox( $post ) { $post_id = ( ! empty( $post ) && isset( $post->ID ) ) ? $post->ID : false; // Get or generate the invite code $all_in_one_invite_code = all_in_one_invite_codes_md5( $post_id ); // Get the invite code options $all_in_one_invite_codes_options = get_post_meta( $post_id, 'all_in_one_invite_codes_options', true ); // Get the default values $all_in_one_invite_codes_options_defaults = all_in_one_invite_codes_options_defaults(); // Merge the options so we have the default take care of the missing values. $all_in_one_invite_codes_options = wp_parse_args( $all_in_one_invite_codes_options, $all_in_one_invite_codes_options_defaults ); $email = isset( $all_in_one_invite_codes_options['email'] ) ? $all_in_one_invite_codes_options['email'] : ''; $generate_codes = isset( $all_in_one_invite_codes_options['generate_codes'] ) ? $all_in_one_invite_codes_options['generate_codes'] : ''; $type = isset( $all_in_one_invite_codes_options['type'] ) ? $all_in_one_invite_codes_options['type'] : 'registration'; $multiple_use = isset( $all_in_one_invite_codes_options['multiple_use'] ) ? 'checked="true"' : ''; ?>

eMail:

Multiple Use: >

Number:

Purpose:

$post_id, 'posts_per_page' => - 1, 'post_type' => 'tk_invite_codes', // you can use also 'any' ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); echo '' . esc_html( get_post_meta( get_the_ID(), 'tk_all_in_one_invite_code', true ) ) . ''; echo '
'; endwhile; endif; wp_reset_postdata(); // add the nonce check wp_nonce_field( 'all_in_one_invite_codes_options_nonce', 'all_in_one_invite_codes_options_process' ); } /** * Save the options * * @param $post_id * @param $post * * @return int */ function all_in_one_invite_codes_save_options( $post_id, $post ) { global $wpdb; $nonce = isset( $_POST['all_in_one_invite_codes_options_process'] ) ? sanitize_text_field( wp_unslash( $_POST['all_in_one_invite_codes_options_process'] ) ) : ''; if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'all_in_one_invite_codes_options_nonce' ) ) { return $post_id; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } if ( ! isset( $_POST['all_in_one_invite_codes_options'] ) || ! is_array( $_POST['all_in_one_invite_codes_options'] ) ) { return $post_id; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each scalar value is run through wp_kses_post in the foreach below; PCP can't follow the array branch. $raw_options = wp_unslash( $_POST['all_in_one_invite_codes_options'] ); $sanitized = array(); foreach ( $raw_options as $key => $detail ) { $sanitized[ sanitize_key( $key ) ] = is_scalar( $detail ) ? wp_kses_post( $detail ) : ''; } // Do the update update_post_meta( $post_id, 'all_in_one_invite_codes_options', $sanitized ); // Check if code is multiuse if ( isset( $sanitized['multiple_use'] ) && isset( $sanitized['email'] ) ) { // If the code is multiple then save the total $code_amount = isset( $sanitized['generate_codes'] ) ? $sanitized['generate_codes'] : 1; $sanitized['code_total'] = $code_amount; update_post_meta( $post_id, 'all_in_one_invite_codes_options', $sanitized ); $asign_user = get_user_by( 'email', $sanitized['email'] ); if ( $asign_user ) { // Direct update keeps us out of the save_post recursion that wp_update_post would trigger. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->update( $wpdb->posts, array( 'post_author' => $asign_user->ID ), array( 'ID' => $post_id ), array( '%d' ), array( '%d' ) ); if ( false === $result ) { return false; } clean_post_cache( $post_id ); } } if ( ! empty( $sanitized['email'] ) && isset( $_POST['tk_all_in_one_invite_code'] ) ) { $email = sanitize_email( $sanitized['email'] ); $type = isset( $sanitized['type'] ) ? sanitize_key( $sanitized['type'] ) : 'any'; switch ( $type ) { case 'any': case 'register': $message_text = 'message_text'; break; default: $message_text = $type; break; } $tk_invite_code = sanitize_key( wp_unslash( $_POST['tk_all_in_one_invite_code'] ) ); $all_in_one_invite_codes_mail_templates = get_option( 'all_in_one_invite_codes_mail_templates' ); $subject = isset( $all_in_one_invite_codes_mail_templates['subject'] ) ? wp_kses_post( $all_in_one_invite_codes_mail_templates['subject'] ) : __( "You've Been Invited!", 'all-in-one-invite-codes' ); $body = isset( $all_in_one_invite_codes_mail_templates[ $message_text ] ) ? wp_kses_post( $all_in_one_invite_codes_mail_templates[ $message_text ] ) : __( 'You got an invite from the site [site_name]. Please use this link to register with your invite code [invite_link]', 'all-in-one-invite-codes' ); $site_name = get_bloginfo( 'name' ); $subject = all_in_one_invite_codes_replace_shortcode( $subject, '[site_name]', $site_name ); $subject = all_in_one_invite_codes_replace_shortcode( $subject, '[invite_code]', $tk_invite_code ); $body = all_in_one_invite_codes_replace_shortcode( $body, '[site_name]', $site_name ); $body = all_in_one_invite_codes_replace_shortcode( $body, '[invite_code]', $tk_invite_code ); // Invite Link $buddypress_active = function_exists( 'bp_is_active' ); $separator = ( $buddypress_active || ! all_in_one_invite_codes_is_default_registration() ) ? '?' : '&'; $invite_link = 'Link'; $subject = all_in_one_invite_codes_replace_shortcode( $subject, '[invite_link]', $invite_link ); $body = all_in_one_invite_codes_replace_shortcode( $body, '[invite_link]', $invite_link ); // sent the mail $headers = array( 'Content-Type: text/html; charset=UTF-8' ); $post_type = isset( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : ''; if ( 'tk_invite_codes' === $post_type ) { $email_param = array( 'to' => $email, 'subject' => $subject, 'body' => $body, 'headers' => $headers, ); $email_param = apply_filters( 'all_in_one_invite_code_custom_email', $email_param ); if ( ! empty( $email ) ) { wp_mail( $email_param['to'], $email_param['subject'], $email_param['body'], $email_param['headers'] ); } } } if ( ! get_post_meta( $post_id, 'tk_all_in_one_invite_code_status', true ) ) { update_post_meta( $post_id, 'tk_all_in_one_invite_code_status', 'Active' ); } return $post_id; } add_action( 'save_post_tk_invite_codes', 'all_in_one_invite_codes_save_options', 1, 2 ); /** * Save the invite code * * @param $post_id * @param $post * * @return mixed */ function all_in_one_invite_codes_save_code( $post_id, $post ) { $nonce = isset( $_POST['all_in_one_invite_codes_options_process'] ) ? sanitize_text_field( wp_unslash( $_POST['all_in_one_invite_codes_options_process'] ) ) : ''; if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'all_in_one_invite_codes_options_nonce' ) ) { return $post_id; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } if ( ! isset( $_POST['tk_all_in_one_invite_code'] ) ) { return $post_id; } $tk_all_in_one_invite_code = get_post_meta( $post_id, 'tk_all_in_one_invite_code', true ); if ( $tk_all_in_one_invite_code ) { return $post_id; } $tk_invite_code = sanitize_key( wp_unslash( $_POST['tk_all_in_one_invite_code'] ) ); update_post_meta( $post_id, 'tk_all_in_one_invite_code', $tk_invite_code ); return $post_id; } add_action( 'save_post', 'all_in_one_invite_codes_save_code', 1, 2 ); /** * Remove slugdiv */ function all_in_one_invite_codes_remove_slugdiv() { remove_meta_box( 'slugdiv', 'tk_invite_codes', 'normal' ); } add_action( 'admin_menu', 'all_in_one_invite_codes_remove_slugdiv' ); add_filter( 'wp_insert_post_data', 'all_in_one_invite_codes_change_title' ); function all_in_one_invite_codes_change_title( $data ) { if ( ! isset( $data['ID'] ) ) { return $data; } $post_id = $data['ID']; $nonce = isset( $_POST['all_in_one_invite_codes_options_process'] ) ? sanitize_text_field( wp_unslash( $_POST['all_in_one_invite_codes_options_process'] ) ) : ''; if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'all_in_one_invite_codes_options_nonce' ) ) { return $data; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return $data; } if ( ! isset( $_POST['tk_all_in_one_invite_code'] ) ) { return $data; } $data['post_title'] = sanitize_key( wp_unslash( $_POST['tk_all_in_one_invite_code'] ) ); return $data; }