Use PHP shortcode value as CSS value - php

I am facing the following problem. On my website I am trying to use a WordPress shortcode as input for a background color gradient.
The PHP is as follows:
function vip_relative_time() {
$post_date = get_the_time('U');
$delta = time() - $post_date;
$delta2 = intval(($delta/86400));
return $delta2;
}
add_shortcode('relativetime', 'vip_relative_time');
This code returns the amount of days that is between today and the post date. The goal is that the section on my website has a color gradient based on the above calculated days.
So when the calculating returns 50, 50% of the section will be filled with the selected color. This seems to work when I am editing the page:
But when I publish the page the the gradient is gone an I get the following error:
Warning
: Illegal string offset 'size' in
/home/customer/www/nicow23.sg-host.com/public_html/wp-content/plugins/elementor/includes/base/controls-stack.php
on line
1251
Anyone who can help me out to fix this problem? Do I maybe need to change the datatype of my shortcode?
Thanks in advance!
The code in controls-stack.php
/**
* Parse dynamic settings.
*
* Retrieve the settings with rendered dynamic tags.
*
* #since 2.0.0
* #access public
*
* #param array $settings Optional. The requested setting. Default is null.
* #param array $controls Optional. The controls array. Default is null.
* #param array $all_settings Optional. All the settings. Default is null.
*
* #return array The settings with rendered dynamic tags.
*/
public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
if ( null === $all_settings ) {
$all_settings = $this->get_settings();
}
if ( null === $controls ) {
$controls = $this->get_controls();
}
foreach ( $controls as $control ) {
$control_name = $control['name'];
$control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
if ( ! $control_obj instanceof Base_Data_Control ) {
continue;
}
if ( $control_obj instanceof Control_Repeater ) {
if ( ! isset( $settings[ $control_name ] ) ) {
continue;
}
foreach ( $settings[ $control_name ] as & $field ) {
$field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
}
continue;
}
$dynamic_settings = $control_obj->get_settings( 'dynamic' );
if ( ! $dynamic_settings ) {
$dynamic_settings = [];
}
if ( ! empty( $control['dynamic'] ) ) {
$dynamic_settings = array_merge( $dynamic_settings, $control['dynamic'] );
}
if ( empty( $dynamic_settings ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
continue;
}
if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
$parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );
$dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
if( is_array( $settings ) )
{
if ( $dynamic_property ) {
$settings[ $control_name ][ $dynamic_property ] = $parsed_value;
} else {
$settings[ $control_name ] = $parsed_value;
}
} else {
$settings[ $control_name ] = $parsed_value;
}
}
}
return $settings;
}

On or before line 1251, you should be checking if $settings is an array before trying to set its keys. If you do a var_dump($settings) prior to line 1251. It will probably tell you that it is not an array, and instead a string.
should be like this:
if( is_array( $settings ) )
{
//make sure we have an array first
if ( $dynamic_property ) {
$settings[ $control_name ][ $dynamic_property ] = $parsed_value;
} else {
$settings[ $control_name ] = $parsed_value;
}
} else {
//do something else
}

Related

Pull out random number of questions like 04 out of 10

I have function previously made which get me a set of questions array each time shuffled in a new set of order each time when i take a quiz with shuffle() function
Original array (Q1,Q2,Q3,Q4,Q5,Q6)
1st time it gives me (Q2,Q4,Q1,Q3,Q6,Q5)
2nd time it gives me (Q3,Q4,Q2,Q6,Q1,Q5) and so on.
everytime a new set of questions.
This is the function code working for only for shuffle
/**
* Update user questions.
*
* #param $quiz_id
* #param $course_id
* #param $user_id
*/
public function update_user_questions( $quiz_id, $course_id, $user_id ) {
global $wpdb;
$item = null;
switch ( current_action() ) {
case 'pmi-course/user/quiz-redone':
$item = $wpdb->get_row(
$wpdb->prepare( "
SELECT * FROM {$wpdb->prefix}pmicourse_user_items
WHERE item_id = %d
AND user_id = %d
AND ref_id = %d
ORDER BY user_item_id DESC
", $quiz_id, $user_id, $course_id )
);
break;
case 'pmi-course/user/quiz-started':
break;
}
if ( ! $item ) {
return;
}
if ( ! $item->status == 'started' ) {
return;
}
$random_quiz = get_user_meta( $user_id, 'random_quiz', true );
$quiz = LP_Quiz::get_quiz( $quiz_id );
if ( $quiz && $questions = $quiz->get_questions() ) {
$questions = array_keys( $questions );
shuffle( $questions );
$question_id = reset( $questions );
// set user current question
$user = pmi_course_get_current_user();
$user_course = $user->get_course_data( $course_id );
$item_quiz = $user_course->get_item($quiz_id);
$item_quiz->set_meta( '_current_question', $question_id );
$item_quiz->update_meta();
pmi_course_update_user_item_meta( $item->user_item_id, 'current_question', $question_id );
if ( empty( $random_quiz ) ) {
$random_quiz = array( $quiz_id => $questions );
} else {
$random_quiz[ $quiz_id ] = $questions;
}
update_user_meta( $user_id, 'random_quiz', $random_quiz );
}
}
/**
* Random quiz questions.
*
* #param $quiz_questions
* #param $quiz_id
*
* #return array
*/
public function random_questions( $quiz_questions, $quiz_id ) {
if ( get_post_meta( $quiz_id, '_lp_random_mode', true ) == 'yes' ) {
// get user meta random quiz
$random_quiz = get_user_meta( get_current_user_id(), 'random_quiz', true );
if ( is_admin() || empty( $random_quiz ) || empty( $random_quiz[ $quiz_id ] ) ) {
return $quiz_questions;
}
$questions = array();
if ( array_key_exists( $quiz_id, $random_quiz ) && sizeof( $random_quiz[ $quiz_id ] ) == sizeof( $quiz_questions ) ) {
foreach ( $random_quiz[ $quiz_id ] as $question_id ) {
if ( $question_id ) {
$questions[ $question_id ] = $question_id;
}
}
} else {
$question_ids = array_keys( $quiz_questions );
shuffle( $question_ids );
$random_quiz[ $quiz_id ] = $question_ids;
$questions = array();
foreach ( $question_ids as $id ) {
$questions[ $id ] = $quiz_questions[ $id ];
}
}
return $questions;
}
return $quiz_questions;
}
This is what i tried to pull out specific number of questions randomly like "4" questions out of "6" questions from a set but it gives the random result one time and not again like shuffle() i know array_rand() gives different value each time but why not in my case. may be i am doing something wrong . I have put down both the case either can anyone identify me what i am doing wrong or the solution could be extended to more better. Any help would be appreciated.
Original array (Q1,Q2,Q3,Q4,Q5,Q6)
First time (Q1,Q4,Q5,Q6)
Second time (Q1,Q4,Q5,Q6)
Third time (Q1,Q4,Q5,Q6) ---i want different sets for each time
What i tried to achieve this
///for first function
if ( $quiz && $questions = $quiz->get_questions() ) {
$questions = array_rand( $questions , 4);
$question_id = reset( $questions );
///for second function
if ( array_key_exists( $quiz_id, $random_quiz ) && sizeof( $random_quiz[ $quiz_id ] ) < sizeof( $quiz_questions ) ) {
foreach ( $random_quiz[ $quiz_id ] as $question_id ) {
if ( $question_id ) {
$questions[ $question_id ] = $question_id;
}
}
} else {
question_ids = array_rand( $quiz_questions , 4);
$random_quiz[ $quiz_id ] = $question_ids;
questions = array();
foreach ( $question_ids as $id ) {
$questions[ $id ] = $quiz_questions[ $id ];
}
}
Simple function to grab a set of random elements from an array:
$questions = array("Question 1","Question 2","Question 3","Question 4","Question 5","Question 6","Question 7","Question 8","Question 9","Question 10");
function shuffle_questions($questions, $num_questions){
$random_questions = [];
for($i = 0; $i < $num_questions){
$random_num = rand(0, 9);
$random_questions[] = $questions[$random_num];
}
return $random_questions;
}
If you only want it to return unique questions (Probably what you're looking for)
function shuffle_questions($questions, $num_questions){
$questions_temp = $questions;
$random_questions = [];
for ($i = 0; $i++; $i < $num_questions){
$key = array_rand($questions_temp, 1);
$random_questions[] = $questions_temp[$key];
$unset($questions_temp[$key]);
}
return $random_questions;
}

Getting this error "Trying to get property of non-object" for a function

This is a function to removed some banners, I know the error is happening around the if ( ! in_array( $current_screen->post_type, $post_types ) ) but I don't know to properly fix it.
function lsx_tec_disable_lsx_banner( $disabled ) {
global $current_screen;
$post_types = apply_filters( 'tribe_is_post_type_screen_post_types', Tribe__Main::get_post_types() );
if ( ! in_array( $current_screen->post_type, $post_types ) ) {
$disabled = true;
}
if ( is_null( $id ) && false !== strpos( $current_screen->id, 'tribe' ) )
{
$disabled = true;
}
if ( is_single() && tribe_is_event() ) {
$disabled = true;
}
return $disabled;
}
please check below link and change your code.
https://codex.wordpress.org/Function_Reference/get_current_screen
<?php
$current_screen = get_current_screen(); // use this instead of global $current_screen;
?>

Can I modify this wordpress plugin function with a action/filter hook?

I need to modify 3 lines in a function created by a plugin.
Here is the original function (look for my commented section in the middle, highlighted by the asterisks):
/**
* function to modify order_items of renewal order
*
* #param array $order_items
* #param int $original_order_id
* #param int $renewal_order_id
* #param int $product_id
* #param string $new_order_role
* #return array $order_items
*/
public function sc_subscriptions_renewal_order_items( $order_items = null, $original_order_id = 0, $renewal_order_id = 0, $product_id = 0, $new_order_role = null ) {
$is_subscription_order = wcs_order_contains_subscription( $original_order_id );
if ( $is_subscription_order ) {
$return = false;
} else {
$return = true;
}
if ( $return ) {
return $order_items;
}
$pay_from_credit_of_original_order = get_option( 'pay_from_smart_coupon_of_original_order', 'yes' );
if ( $pay_from_credit_of_original_order != 'yes' ) return $order_items;
if ( $new_order_role != 'child' ) return $order_items;
if ( empty( $renewal_order_id ) || empty( $original_order_id ) ) return $order_items;
$original_order = $this->get_order( $original_order_id );
$renewal_order = $this->get_order( $renewal_order_id );
$coupon_used_in_original_order = $original_order->get_used_coupons();
$coupon_used_in_renewal_order = $renewal_order->get_used_coupons();
if ( sizeof( $coupon_used_in_original_order ) > 0 ) {
$smart_coupons_contribution = array();
foreach ( $coupon_used_in_original_order as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code );
if ( ! empty( $coupon->discount_type ) && $coupon->discount_type == 'smart_coupon' && ! empty( $coupon->amount ) && ! in_array( $coupon_code, $coupon_used_in_renewal_order, true ) ) {
$renewal_order_total = $renewal_order->get_total();
/* ************
THE BELOW 3 LINES ARE WHAT I NEED TO REMOVE
**************** */
if ( $coupon->amount < $renewal_order_total ) {
continue;
}
/* ************
THE ABOVE 3 LINES ARE WHAT I NEED TO REMOVE
**************** */
$discount = min( $renewal_order_total, $coupon->amount );
if ( $discount > 0 ) {
$new_order_total = $renewal_order_total - $discount;
update_post_meta( $renewal_order_id, '_order_total', $new_order_total );
update_post_meta( $renewal_order_id, '_order_discount', $discount );
if ( $new_order_total <= floatval(0) ) {
update_post_meta( $renewal_order_id, '_renewal_paid_by_smart_coupon', 'yes' );
}
$renewal_order->add_coupon( $coupon_code, $discount );
$smart_coupons_contribution[ $coupon_code ] = $discount;
$used_by = $renewal_order->get_user_id();
if ( ! $used_by ) {
$used_by = $renewal_order->billing_email;
}
$coupon->inc_usage_count( $used_by );
}
}
}
if ( ! empty( $smart_coupons_contribution ) ) {
update_post_meta( $renewal_order_id, 'smart_coupons_contribution', $smart_coupons_contribution );
}
}
return $order_items;
}
I want to remove the lines that I commented out in the middle of the above code:
if ( $coupon->amount < $renewal_order_total ) {
continue;
}
Is there any way to do this without editing the core plugin code? Could I write my own function to change those lines?
Thank you for any help you can provide!
If this function is hooked in with an action/filter hook then you can remove it by remove_action or remove_filter and hook in your function there.
https://codex.wordpress.org/Function_Reference/remove_action
https://codex.wordpress.org/Function_Reference/remove_filter
So check where this function gets called.

URL dissector that splits up a query string

Ok so basically I am reading through this piece of source code and do not understand the purpose of a specific area.
class URL_Processor
{
private static $urlPath;
private static $urlBits = array();
/*
Gets data from the current URL
#return Void
*/
public function getURLData()
{
$urldata = (isset($_GET['page'])) ? $_GET['page'] : '' ;
self::$urlPath = $urldata;
if( $urldata == '' )
{
self::$urlBits[] = 'home';
self::$urlPath = 'home';
}
else
{
$data = explode( '/', $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
self::$urlBits = $this->array_trim( $data );
}
}
private function array_trim( $array )
{
while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
{
array_shift( $array );
}
while ( !empty( $array ) && strlen( end( $array ) ) === 0)
{
array_pop( $array );
}
return $array;
}
}
So basically from my understanding the two while loops with 'array_shift' in the getURLData method empty out the array but according to my logic the second while loop wont even be able to empty anything out because the first while loop already did.
Then the last line of the method getURLData
self::$urlBits = $this->array_trim( $data );
does the same thing but how if the passed in argument is empty already?
Very confused!!!
The first while loop removes all leading elements in the array where their string length is zero, the second one does the same with trailing elements. reset($array) will point to the first, end($array) to the last element.
Why he mushes it through a second time? I don't know.

need help on a piece of code from PHP 5 Social Networking

I'm trying to get more advanced with php and I pick up the book PHP 5 Social Networking by Michael Peacock. While the book seemed to be interesting it didn't however get to involved in the details of the code. The function I'm trying to figure out is,
public function getURLData()
{
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
$data = explode( '/', $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
This a part of a larger class and the $_GET['page'] is something like this: relationships/mutual/3. My main question is what is happening in the else section. I think what is happening that it's removing any empty array indexes but I also question that.
Any help would be appreciated.
EDIT: added array_trim function that is also part of the class
private function array_trim( $array )
{
while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
{
array_shift( $array );
}
while ( !empty( $array ) && strlen( end( $array ) ) === 0)
{
array_pop( $array );
}
return $array;
}
public function getURLData()
{
Gets the 'page', this data can be obtained by $_GET from the url: for instance: http://mysite.com/?page=contact
If 'page' has been set, is assigned to $urldata, else $urldata=''
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
Now is creating an array with all the substrings from $urldata splited by '/'
$data = explode( '/', $urldata );
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the first element is equal to 0, then removes the first element from the array.
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the last element is equal to 0, then removes the last element from the array.
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
array_trim is a custom function, not sure what does but probably will do some kind of trimming too
$this->urlBits = $this->array_trim( $data );
}
}

Categories