Make WordPress Core

Opened 20 years ago

Closed 19 years ago

#1226 closed defect (bug) (duplicate)

apply_filters() does not pass argument to template functions

Reported by: mani_monaj's profile mani_monaj Owned by: ryan's profile ryan
Milestone: Priority: normal
Severity: major Version: 1.5
Component: Template Keywords:
Focuses: Cc:

Description

I've been working on WordPress localization for persian(iranian) users for a while , As you may heard persians(iranians) calendar is based on Jalali's Calendar, starting from first day of spring ... My biggest aim was converting wp 1.2 & wp 1.5 dates, calendars and archives to jalali calendar based on Georgian2Jalali convertion engine written by farsiweb.info .

I found a little bug with major effects in WP 1.5 plugin core and i wanted u to mention , As I found in WP 1.5 u decided to pass the template tags arguments to plug-ins filters (unlike WP 1.2) , for example if the_time() tag had and argument the filter for the_time() could be able to access the arguments. but the function apply_filter() does not function properly.

Let me describe more :

this is the source of apply_filters :

function apply_filters($tag, $string) {

global $wp_filter;
$args = array_slice(func_get_args(), 3);
merge_filters($tag);
if (isset($wp_filter[$tag])) {

foreach ($wp_filter[$tag] as $priority => $functions) {

if (!is_null($functions)) {

foreach($functions as $function) {

$string = call_user_func_array($function, array($string) + $args);

}

}

}

}
return $string;

}

the first problem is line 2 of the function with array_slice , if we want to get the extra arguments which sent to this function other than $tag and $string we should set that 3 to 2.

The second problem is in the line $string = call_user_func_array($function, array($string) + $args);
if we want to merge two arrays we should use array_merge instead, then when we merge these two arrays to pass to plug-in function, empty values(i don't know where they come from) cause critical problems, so i decided to to clean the arguments array first ,

I wrote out the new function below , if u want to see the bug before u test this function , first write a simple fake plug-in like this

function fake_the_time($the_time, $d=) {

return "WE WANTED TO FORMAT THE TIME WITH $d and the result is $the_time";

}
add_filter("the_time","fake_the_time");

with the wordpress 1.5 default plug-in with the_time(format string) this plug-in would not return $d in output, because it doesn't have the argument(s) at all!

this is the new function :
function apply_filters($tag, $string) {

global $wp_filter;


$args = array_slice(func_get_args(), 3);
$args = array_slice(func_get_args(), 2);


merge_filters($tag);


if (isset($wp_filter[$tag])) {

foreach ($wp_filter[$tag] as $priority => $functions) {

if (!is_null($functions)) {

ADDED BY ME
foreach($functions as $function) {

$args_to_pass = array($string);
foreach ($args as $arg) {

if (!empty($arg)) $args_to_pass[] = $arg;

}
$string = call_user_func_array($function, array($string)+$args);
$string = call_user_func_array($function, $args_to_pass);

}

}

}

}
return $string;

}

I am in sort of hurry to publish the project results and i didn't have any CSV experience, so please test this and let me know about the results, I am really looking forward to the reply.

Change History (2)

#1 @mani_monaj
20 years ago

  • Patch set to No

#2 @ryan
19 years ago

  • duplicate_id changed from 0 to 901
  • Owner changed from anonymous to rboren
  • Resolution changed from 10 to 60
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.