Skip to content

Commit

Permalink
merge #786
Browse files Browse the repository at this point in the history
  • Loading branch information
bduranleau-nr committed Dec 1, 2023
1 parent 84ef834 commit db04572
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 28 deletions.
59 changes: 31 additions & 28 deletions agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,33 +427,34 @@ NR_PHP_WRAPPER(nr_wordpress_exec_handle_tag) {
NR_UNUSED_SPECIALFN;
(void)wraprec;

NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_WORDPRESS);
if (nrlikely(0 != NRINI(wordpress_hooks))) {
NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_WORDPRESS);

tag = nr_php_arg_get(1, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
tag = nr_php_arg_get(1, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);

if (1 == nr_php_is_zval_non_empty_string(tag)
|| (0 != NRINI(wordpress_hooks))) {
/*
* Our general approach here is to set the wordpress_tag global, then let
* the call_user_func_array instrumentation take care of actually timing
* the hooks by checking if it's set.
*/
char* old_tag = NRPRG(wordpress_tag);
if (1 == nr_php_is_zval_non_empty_string(tag)) {
/*
* Our general approach here is to set the wordpress_tag global, then let
* the call_user_func_array instrumentation take care of actually timing
* the hooks by checking if it's set.
*/
char* old_tag = NRPRG(wordpress_tag);

NRPRG(check_cufa) = true;
NRPRG(check_cufa) = true;

NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
NR_PHP_WRAPPER_CALL;
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
NR_PHP_WRAPPER_CALL;
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(check_cufa) = false;
}
} else {
NRPRG(check_cufa) = false;
NR_PHP_WRAPPER_CALL;
}
} else {
NRPRG(check_cufa) = false;
NR_PHP_WRAPPER_CALL;
}

nr_php_arg_release(&tag);
nr_php_arg_release(&tag);
}
}
NR_PHP_WRAPPER_END

Expand Down Expand Up @@ -553,17 +554,19 @@ void nr_wordpress_enable(TSRMLS_D) {
nr_php_wrap_user_function(NR_PSTR("apply_filters"),
nr_wordpress_apply_filters TSRMLS_CC);

nr_php_wrap_user_function(NR_PSTR("apply_filters_ref_array"),
nr_wordpress_exec_handle_tag TSRMLS_CC);
if (0 != NRINI(wordpress_hooks)) {
nr_php_wrap_user_function(NR_PSTR("apply_filters_ref_array"),
nr_wordpress_exec_handle_tag TSRMLS_CC);

nr_php_wrap_user_function(NR_PSTR("do_action"),
nr_wordpress_exec_handle_tag TSRMLS_CC);
nr_php_wrap_user_function(NR_PSTR("do_action"),
nr_wordpress_exec_handle_tag TSRMLS_CC);

nr_php_wrap_user_function(NR_PSTR("do_action_ref_array"),
nr_wordpress_exec_handle_tag TSRMLS_CC);
nr_php_wrap_user_function(NR_PSTR("do_action_ref_array"),
nr_wordpress_exec_handle_tag TSRMLS_CC);

nr_php_add_call_user_func_array_pre_callback(
nr_wordpress_call_user_func_array TSRMLS_CC);
nr_php_add_call_user_func_array_pre_callback(
nr_wordpress_call_user_func_array TSRMLS_CC);
}
}

void nr_wordpress_minit(void) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should name WordPress web transaction as an 'Action' with the
name generated from the template used to generate the page even when
WordPress hooks are disabled.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = false
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST
Supportability/InstrumentedFunction/apply_filters
WebTransaction/Action/template
*/

/*EXPECT_ERROR_EVENTS null */

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func($wp_filters[$hook_name], $value);
}

// Hook 'template_include' with an identity filter
function identity_filter($value) {
return $value;
}
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should name WordPress web transaction as an 'Action' with the
name generated from the template used to generate the page. Additionally
since WordPress hooks are enabled, Framework/WordPress/Hook/template_include
metric should be generated and the hook function should be instrumented.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = true
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST
Framework/WordPress/Hook/template_include
Supportability/InstrumentedFunction/add_filter
Supportability/InstrumentedFunction/identity_filter
Supportability/InstrumentedFunction/apply_filters
WebTransaction/Action/template
*/

/*EXPECT_ERROR_EVENTS null */

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func($wp_filters[$hook_name], $value);
}

// Hook 'template_include' with an identity filter
function identity_filter($value) {
return $value;
}
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");

0 comments on commit db04572

Please sign in to comment.