_config = Dispatcher::config(); } /** * Runs the NewRelic extension and completes necessary setup. * * @return void */ public function run() { add_filter( 'w3tc_config_default_values', array( $this, 'w3tc_config_default_values' ) ); $config = Dispatcher::config(); // remainder only when extension is frontend-active. if ( ! $config->is_extension_active_frontend( 'newrelic' ) ) { return; } if ( 'browser' === $this->_config->get_string( array( 'newrelic', 'monitoring_type' ) ) ) { Util_Bus::add_ob_callback( 'newrelic', array( $this, 'ob_callback_browser' ) ); } else { require_once W3TC_LIB_NEWRELIC_DIR . '/NewRelicWrapper.php'; $this->set_appname(); if ( defined( 'DOING_CRON' ) && DOING_CRON ) { $this->background_task(); } Util_Bus::add_ob_callback( 'newrelic', array( $this, 'ob_callback_apm' ) ); } add_filter( 'w3tc_footer_comment', array( $this, 'w3tc_footer_comment' ) ); } /** * Sets default configuration values for the NewRelic extension. * * @param array $default_values Default configuration values. * * @return array Modified default configuration values. */ public function w3tc_config_default_values( $default_values ) { $default_values['newrelic'] = array( 'monitoring_type' => 'apm', 'accept.logged_roles' => true, 'accept.roles' => array( 'contributor' ), 'use_php_function' => true, 'cache_time' => 5, 'include_rum' => true, ); return $default_values; } /** * Handles the output buffer for browser-based monitoring. * * @param string $buffer The output buffer content. * * @return string The modified output buffer. */ public function ob_callback_browser( $buffer ) { $core = Dispatcher::component( 'Extension_NewRelic_Core' ); $app = $core->get_effective_browser_application(); if ( isset( $app['loader_script'] ) && $this->_can_add_tracker_script( $buffer ) ) { $buffer = preg_replace( '~
]*)*>~Ui', '\\0' . $app['loader_script'], $buffer, 1 ); } $buffer = str_replace( '{w3tc_newrelic_reject_reason}', ( '' !== $this->newrelic_reject_reason ? sprintf( ' (%s)', $this->newrelic_reject_reason ) : '' ), $buffer ); return $buffer; } /** * Handles the output buffer for APM-based monitoring. * * @param string $buffer The output buffer content. * * @return string The modified output buffer. */ public function ob_callback_apm( $buffer ) { if ( ! $this->_can_add_tracker_script( $buffer ) ) { $this->disable_auto_rum(); } elseif ( $this->_config->get_boolean( array( 'newrelic', 'include_rum' ) ) ) { $buffer = preg_replace( '~]*)*>~Ui', '\\0' . \NewRelicWrapper::get_browser_timing_header(), $buffer, 1 ); $buffer = preg_replace( '~<\\/body>~', \NewRelicWrapper::get_browser_timing_footer() . '\\0', $buffer, 1 ); } $buffer = str_replace( '{w3tc_newrelic_reject_reason}', ( '' !== $this->newrelic_reject_reason ? sprintf( ' (%s)', $this->newrelic_reject_reason ) : '' ), $buffer ); return $buffer; } /** * Marks the current job as a background task. * * @return void */ public function background_task() { \NewRelicWrapper::mark_as_background_job(); } /** * Disables automatic RUM (Real User Monitoring) for the current request. * * @return void */ public function disable_auto_rum() { \NewRelicWrapper::disable_auto_rum(); } /** * Checks whether a tracker script can be added to the output buffer. * * @param string $buffer The output buffer content. * * @return bool Whether the tracker script can be added. */ public function _can_add_tracker_script( $buffer ) { $v = ''; if ( preg_match( '~^\s*<\?xml[^>]*>\s*