WPSC_UPDATE_META Function in PHP How too use it? - php

Someone that's familiar with this know how to use it? I can't find any documentation on this function anywhere. and can this be used to update say for example the weight field and changing it from the default of pounds to options?
I assume the proper setup would be something like this, bu I've no idea what goes in type.
wpsc_update_meta($post_id, 'weight', '3', $type);
wpsc_update_meta($post_id, 'weight_unit', 'ounce', $type);
Any ideas guys? The full function is listed below.
function wpsc_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
global $wpdb;
if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
return false;
}
$cache_object_id = $object_id = (int) $object_id;
$object_type = $type;
$meta_key = wpsc_sanitize_meta_key( $meta_key );
$meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
$meta_tuple = apply_filters( 'wpsc_update_meta', $meta_tuple );
extract( $meta_tuple, EXTR_OVERWRITE );
$meta_value = $_meta_value = maybe_serialize( $meta_value );
$meta_value = maybe_unserialize( $meta_value );
$cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_META."` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) );
if ( !$cur ) {
$wpdb->insert( WPSC_TABLE_META, array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $_meta_value ) );
} elseif ( $cur->meta_value != $meta_value ) {
$wpdb->update( WPSC_TABLE_META, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
}
wp_cache_delete( $cache_object_id, $object_type );
if ( !$cur ) {
return true;
}
}

I'm not familiar with e-Commerce plugin, but I assume that this function is some kind of their own implementation of the WP update_metadata() function.
If that's true, then $type param should have the same meaning as the $meta_type param at update_metadata() (e.g. post_type, taxonomy...). Also look at the wp_cache_delete() usage at the bottom. This could give you some additional hints.

Related

Relating Wordpress Users with Order Export

We are using Advanced Custom Fields, WP All Export Pro, and WordPress with Woocommerce.
I have an ACF field created for our accountant in users area of WordPress called "traverse_customer_id". This is for her to know what customer this order is for.
I have this ACF field showing in the orders export but the data comes back blank on each export. I think it's because I'm not relating the order with the customer or user ID. I've attempted to use a code snippet from WP All Exports' support without any luck. Could anyone with knowledge help me see what I'm doing wrong?
This is what I have in the "Custom export field" area: (I think this is running the function on each line of the import.)
[sf_helper_meta_query_lookup("traverse_customer_id",{Customer User ID},"user")]
Then I have this below in the Function Editor:
function sf_helper_meta_query_lookup( $meta_key = '', $meta_value = '', $object_type = 'post', $taxonomy = '', $return_field = 'ID', $compare = '=', $custom_args = array() ) {
if ( empty( $object_type ) || empty( $meta_key ) || empty( $meta_value ) ) return;
$func = 'get_posts';
switch ( $object_type ) {
case 'user':
$func = 'get_users';
break;
case 'term':
$func = 'get_terms';
if ( $return_field == 'ID' ) {
$return_field = 'term_id';
}
break;
default:
$func = 'get_posts';
break;
}
if ( ! empty( $custom_args ) ) {
$objects = $func( $custom_args );
if ( ! empty( $objects ) ) {
return $objects[0]->$return_field;
} else {
return false;
}
}
$args = array();
$meta_query = array( array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => $compare
) );
if ( $func == 'get_terms' ) {
$args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'meta_query' => $meta_query
);
} elseif ( $func == 'get_users' ) {
$args = array(
'meta_query' => $meta_query
);
} else {
$args = array(
'post_type' => $object_type,
'meta_query' => $meta_query
);
}
if ( $objects = $func( $args ) ) {
return $objects[0]->$return_field;
} else {
return false;
}
}
Any help would be greatly appreciated!

How do I show custom field in Sensei LMS Certificates plugin PDF

I am using "Sensei LMS" and "Sensei LMS Certificates" plugins.
Now , to enhance "Sensei LMS Certificates" plugin functionality , I have created a custom meta box and custom fields like this : https://prnt.sc/104s7oy
I am using this action hook sensei_certificates_before_pdf_output to show text in PDF
I have added the following code in my custom plugin :
<?php
add_action( 'sensei_certificates_before_pdf_output', 'action__sensei_certificates_before_pdf_output' , 20, 2 );
function action__sensei_certificates_before_pdf_output( $pdf_certificate, $fpdf ) {
global $woothemes_sensei_certificates;
$show_border = apply_filters( 'woothemes_sensei_certificates_show_border', 0 );
$start_position = 200;
$args = array(
'post_type' => 'certificate',
'meta_key' => 'certificate_hash',
'meta_value' => $pdf_certificate->hash,
);
$query = new WP_Query( $args );
$certificate_id = 0;
if ( $query->have_posts() ) {
$query->the_post();
$certificate_id = $query->posts[0]->ID;
}
wp_reset_query();
if ( 0 < intval( $certificate_id ) ) {
$user_id = get_post_meta( $certificate_id, 'learner_id', true );
$student = get_userdata( $user_id );
$student_name = $student->display_name;
$fname = $student->first_name;
$lname = $student->last_name;
if ( '' != $fname && '' != $lname ) {
$student_name = $fname . ' ' . $lname;
}
$course_id = get_post_meta( $certificate_id, 'course_id', true );
$course_title = get_post_field('post_title', $course_id);
$course_end = Sensei_Utils::sensei_check_for_activity(
array(
'post_id' => intval( $course_id ),
'user_id' => intval( $user_id ),
'type' => 'sensei_course_status',
),
true
);
$course_end_date = $course_end->comment_date;
$date = Woothemes_Sensei_Certificates_Utils::get_certificate_formatted_date( $course_end_date );
if ( isset( $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'] ) && '' != $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'] ) {
$certificate_custom_message = $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'];
$certificate_custom_message .= str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course_title, $date, get_bloginfo( 'name' ) ), $certificate_custom_message );
}
$output_fields = array(
'certificate_custom_message' => 'textarea_field',
);
foreach ( $output_fields as $meta_key => $function_name ) {
if ( isset( $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['x1'] ) ) {
$font_settings = $woothemes_sensei_certificates->get_certificate_font_settings( $meta_key );
call_user_func_array( array( $pdf_certificate, $function_name ), array( $fpdf, $meta_key, $show_border, array( $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['x1'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['y1'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['width'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['height'] ), $font_settings ) );
}
}
} else {
wp_die( esc_html__( 'The certificate you are searching for does not exist.', '' ), esc_html__( 'Certificate Error', '' ) );
}
}
but this is how text showing inside PDF : https://prnt.sc/104sg3v
expected result is "Hello Test hii" instead of "certificate_custom_message"
How do I fix this ?

add "duplicate" to custom post type admin menu

I'm trying to add the "duplicate" post option to my custom post type admin menu called events. I have searched online, but there is very little documentation on how to add this function to the custom post types. Perhaps I am using the incorrect terminology when searching.
The code below adds the "duplicate" function, but when clicked it doesn't actually duplicate the post but returns a white screen instead. Are there any pointers or tips that you can give me?
function rd_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts') || $post->post_type=='events') {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2)
You need to call admin_action_rd_duplicate_post_as_draft hook
function rd_duplicate_post_link( $actions, $post ) {
//print_r($actions);
//if (current_user_can('edit_posts') || $post->post_type=='movies') {
$actions['duplicate'] = 'Duplicate';
// }
return $actions;
}
add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);
add_action( 'admin_action_rd_duplicate_post_as_draft', 'dt_dpp_post_as_draft' );
function dt_dpp_post_as_draft()
{
global $wpdb;
/*sanitize_GET POST REQUEST*/
$post_copy = sanitize_text_field( $_POST["post"] );
$get_copy = sanitize_text_field( $_GET['post'] );
$request_copy = sanitize_text_field( $_REQUEST['action'] );
$opt = get_option('dpp_wpp_page_options');
$suffix = !empty($opt['dpp_post_suffix']) ? ' -- '.$opt['dpp_post_suffix'] : '';
$post_status = !empty($opt['dpp_post_status']) ? $opt['dpp_post_status'] : 'draft';
$redirectit = !empty($opt['dpp_post_redirect']) ? $opt['dpp_post_redirect'] : 'to_list';
if (! ( isset( $get_copy ) || isset( $post_copy ) || ( isset($request_copy) && 'dt_dpp_post_as_draft' == $request_copy ) ) ) {
wp_die('No post!');
}
$returnpage = '';
/* Get post id */
$post_id = (isset($get_copy) ? $get_copy : $post_copy );
$post = get_post( $post_id );
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*Create the post Copy */
if (isset( $post ) && $post != null) {
/* Post data array */
$args = array('comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $post_status,
'post_title' => $post->post_title.$suffix,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$new_post_id = wp_insert_post( $args );
$taxonomies = get_object_taxonomies($post->post_type);
if(!empty($taxonomies) && is_array($taxonomies)):
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);}
endif;
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
/*choice redirect */
if($post->post_type != 'post'):$returnpage = '?post_type='.$post->post_type; endif;
if(!empty($redirectit) && $redirectit == 'to_list'):wp_redirect( admin_url( 'edit.php'.$returnpage ) );
elseif(!empty($redirectit) && $redirectit == 'to_page'):wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
else:
wp_redirect( admin_url( 'edit.php'.$returnpage ) );
endif;
exit;
} else {
wp_die('Error! Post creation failed: ' . $post_id);
}
}

Quotes missing when saving json data inside a database (ajax related)

I noticed that I'm missing the usual quotes when using JSON data inside a database table. This only happens inside the second column Followers IDs
My code is as follows:
public function ajax_follow_me() {
check_ajax_referer( 'km-ajax-create-nonce', 'security' );
$current_user = get_current_user_id();
$target_user = isset( $_POST['data-follow-user'] ) ? $_POST['data-follow-user'] : false;
if( ! empty( $_POST['data-follow-user'] ) ) {
$this->follow_user( $current_user, $target_user );
}
wp_die();
}
Next step...
public function follow_user( $current_user = 0, $user_to_follow = 0 ) {
if ( empty( $user_to_follow ) ) {
return;
}
$args = array(
'user_id' => $current_user,
'follow_to' => $user_to_follow
);
$response = $this->add_following( $args );
if ( ! empty( $response ) && $response !== FALSE ) {
$args = array(
'user_id' => $user_to_follow,
'followed_by' => $current_user
);
$this->add_followed_by( $args );
$this->km_follow_me_success( $user_to_follow );
} else {
$this->km_follow_me_error();
}
}
First column:
public function add_following ( $args = array() ) {
global $wpdb;
$author_info = get_userdata( $args["user_id"] );
if ( empty( $author_info->display_name ) ) {
$author_name = $author_info->user_login;
} else {
$author_name = $author_info->display_name;
}
$defaults = array(
'user_id' => '',
'follow_to' => '',
'username' => $author_name,
'user_email' => $author_info->user_email
);
$args = wp_parse_args( $args, $defaults );
// First you have to check if the user already exists and return his Following Row
$following = array();
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT following FROM {$this->table} WHERE user_id = %d", $args["user_id"] ) );
if( ! empty( $existing ) ) {
$following = json_decode( $existing, true );
}
// We check if this user ($args["user_id"]) is already following the $args["follow_to"] user.
if( in_array( $args["follow_to"], $following ) ) {
return TRUE;
} else{
array_push( $following, $args['follow_to'] );
}
// We verify if the user exists and update the value, if he does not and we sent username, then, we create it.
if( null === $existing && ! empty( $args['username'] ) ) {
$wpdb->insert( $this->table,
array(
'user_id' => $args['user_id'],
'username' => $args['username'],
'email' => $args['user_email'],
'following' => json_encode( $following ),
'followers' => json_encode( array() )
),
array( '%d', '%s', '%s', '%s', '%s' )
);
return $wpdb->insert_id;
} else {
$updated = $wpdb->update( $this->table,
array(
'following' => json_encode( $following )
),
array(
'user_id' => $args["user_id"]
),
'%s', '%d'
);
return $updated;
}
}
This code is for updating the second column (and where it's suppose to go wrong somewhere).
public function add_followed_by( $args = array() ) {
global $wpdb;
$author_info = get_userdata( $args['user_id'] );
if ( empty( $author_info->display_name ) ) {
$author_name = $author_info->user_login;
} else {
$author_name = $author_info->display_name;
}
$defaults = array(
'user_id' => '',
'followed_by' => '',
'username' => $author_name,
'user_email' => $author_info->user_email
);
$args = wp_parse_args( $args, $defaults );
// First you have to check if the user already exists and return his Followers Row
$followers = array();
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT followers FROM {$this->table} WHERE user_id = %d", $args["user_id"] ) );
if( ! empty( $existing ) ) {
$followers = json_decode( $existing, true );
}
// We check if this user ($args["user_id"]) is already followed by $args["followed_by"] user.
if( in_array( $args['followed_by'], $followers ) ) {
return TRUE;
} else {
array_push( $followers, $args['followed_by'] );
}
// We verify if the user exists and update the value, if he does not and we sent username, then, we create it.
if( null === $existing && ! empty( $args['username'] ) ) {
$wpdb->insert( $this->table,
array(
'user_id' => $args['user_id'],
'username' => $args['username'],
'email' => $args['user_email'],
'following' => json_encode( array() ),
'followers' => json_encode( $followers )
),
array( '%d', '%s', '%s', '%s', '%s' )
);
return $wpdb->insert_id;
} else {
$updated = $wpdb->update( $this->table,
array(
'followers' => json_encode( $followers )
),
array(
'user_id' => $args["user_id"]
),
"%s", "%d"
);
return $updated;
}
}
Any help in the right direction is greatly appreciated.

How to exclude a term from a taxonomy post count function

I have a function that displays the post count of any terms for its taxonomy anywhere i want to show it and it works great but i would like to change this up a bit to completely exclude the post count of any post that has a certain term added to it, let's call that "state". So if a post has "state" added to it, i would like the function to not include the count of that post in any terms within the taxonomy.
function get_city_count( $taxonomy = 'countries', $term = '', $args = [] )
{
if ( !$term )
return false;
if ( $term !== 'all' ) {
if ( !is_array( $term ) ) {
$term = filter_var( $term, FILTER_VALIDATE_INT );
} else {
$term = filter_var_array( $term, FILTER_VALIDATE_INT );
}
}
if ( $taxonomy !== 'countries' ) { $taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !taxonomy_exists( $taxonomy ) ) return false;
}
if ( $args ) { if ( !is_array ) return false; } $defaults = [ 'posts_per_page' => 1, 'fields' => 'ids' ];
if ( $term !== 'all' ) { $defaults['tax_query'] = [ [ 'taxonomy' => $taxonomy, 'terms' => $term ] ]; }
$combined_args = wp_parse_args( $args, $defaults );
$q = new WP_Query( $combined_args );
return $q->found_posts;
}

Categories