Skip to content

Commit

Permalink
Better symlink support (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur authored Nov 27, 2019
1 parent 1a6b9a4 commit 52d5bfa
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.2.11
- Better support when a WordPress directory is symlinked

0.2.10
- Improvements for the system report
- Apache htaccess change fixing HTML were not loaded on some installs
Expand Down
28 changes: 24 additions & 4 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,34 @@
define( 'PIWIK_ENABLE_ERROR_HANDLER', false );
}

$was_loaded_directly = ! defined( 'ABSPATH' );
$matomo_was_wp_loaded_directly = ! defined( 'ABSPATH' );

if ( $was_loaded_directly ) {
if ( $matomo_was_wp_loaded_directly ) {
// prevent from loading twice
require_once( dirname( __FILE__ ) . '/../../../../wp-load.php' );
$matomo_wpload_base = '../../../../wp-load.php';
$matomo_wpload_full = dirname( __FILE__ ) . '/' . $matomo_wpload_base;

if (!empty($_ENV['MATOMO_WP_ROOT_PATH']) && file_exists( rtrim($_ENV['MATOMO_WP_ROOT_PATH'], '/') . '/wp-load.php')) {
require_once rtrim($_ENV['MATOMO_WP_ROOT_PATH'], '/') . '/wp-load.php';
} elseif ( file_exists($matomo_wpload_full ) ) {
require_once $matomo_wpload_full;
} elseif (realpath( $matomo_wpload_full ) && file_exists(realpath( $matomo_wpload_full ))) {
require_once realpath( $matomo_wpload_full );
} elseif (!empty($_SERVER['SCRIPT_FILENAME']) && file_exists($_SERVER['SCRIPT_FILENAME'])) {
// seems symlinked... eg the wp-content dir or wp-content/plugins dir is symlinked from some very much other place...
$matomo_wpload_full = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $matomo_wpload_base;
if ( file_exists($matomo_wpload_full ) ) {
require_once $matomo_wpload_full;
} elseif (realpath( $matomo_wpload_full ) && file_exists(realpath( $matomo_wpload_full ))) {
require_once realpath( $matomo_wpload_full );
} elseif (file_exists(dirname(dirname(dirname(dirname(dirname( $_SERVER['SCRIPT_FILENAME'] ))))) . '/wp-load.php')) {
require_once dirname(dirname(dirname(dirname(dirname( $_SERVER['SCRIPT_FILENAME'] ))))) . '/wp-load.php';
}
}
}

if ( ! defined( 'ABSPATH' ) ) {
echo 'Could not find wp-load. If your server uses symlinks or a custom content directory, Matomo may not work for you as we cannot detect the paths correctly. For more information see https://matomo.org/faq/wordpress/what-are-the-requirements-for-matomo-for-wordpress/';
exit; // if accessed directly
}

Expand All @@ -29,7 +49,7 @@
exit;
}

if ($was_loaded_directly) {
if ($matomo_was_wp_loaded_directly) {
// do not strip slashes if we bootstrap matomo within a regular wordpress request
if (!empty($_GET)) {
$_GET = stripslashes_deep( $_GET );
Expand Down
2 changes: 1 addition & 1 deletion classes/WpMatomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function () {
add_action(
'admin_notices',
function () {
echo '<div class="error"><p>' . __( 'It looks like you are maybe using a custom WordPress content directory. The Matomo Analytics plugin likely won\'t fully work.', 'matomo' ) . '</p></div>';
echo '<div class="error"><p>' . __( 'It looks like you are maybe using a custom WordPress content directory. The Matomo reporting/admin pages might not work. You may be able to workaround this.', 'matomo' ) . ' <a target="_blank" rel="noreferrer noopener" href="https://matomo.org/faq/wordpress/what-are-the-requirements-for-matomo-for-wordpress/">'. esc_html__('Learn more', 'matomo').'</a>.</p></div>';
}
);
}
Expand Down
16 changes: 16 additions & 0 deletions classes/WpMatomo/Admin/SystemReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ private function get_matomo_info() {
'comment' => $tmp_dir,
);

if ( ! empty( $_ENV['MATOMO_WP_ROOT_PATH'] ) ) {
$custom_path = rtrim($_ENV['MATOMO_WP_ROOT_PATH'], '/') . '/wp-load.php';
$path_exists = file_exists($custom_path );
$comment = '';
if (!$path_exists) {
$comment = 'It seems the path does not point to the WP root directory.';
}

$rows[] = array(
'name' => 'Custom MATOMO_WP_ROOT_PATH',
'value' => $path_exists,
'is_error' => ! $path_exists,
'comment' => $comment,
);
}

if ( ! \WpMatomo::is_safe_mode() ) {
try {
Bootstrap::do_bootstrap();
Expand Down
6 changes: 4 additions & 2 deletions matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Most powerful web analytics for WordPress giving you 100% data ownership and privacy protection
* Author: Matomo
* Author URI: https://matomo.org
* Version: 0.2.10
* Version: 0.2.11
* Domain Path: /languages
* WC requires at least: 2.4.0
* WC tested up to: 3.2.6
Expand Down Expand Up @@ -36,7 +36,9 @@
$GLOBALS['MATOMO_PLUGIN_FILES'] = array( MATOMO_ANALYTICS_FILE );

function matomo_has_compatible_content_dir() {
return defined( 'WP_CONTENT_DIR' ) && ABSPATH . 'wp-content' === rtrim( WP_CONTENT_DIR, '/' );
return (defined( 'WP_CONTENT_DIR' )
&& ABSPATH . 'wp-content' === rtrim( WP_CONTENT_DIR, '/' ))
|| ( !empty( $_ENV['MATOMO_WP_ROOT_PATH'] ) && is_dir( $_ENV['MATOMO_WP_ROOT_PATH'] ) );
}

function matomo_is_app_request() {
Expand Down
4 changes: 4 additions & 0 deletions plugins/WordPress/Commands/GenerateCoreAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

if (!defined( 'ABSPATH')) {
exit; // if accessed directly
}

class GenerateCoreAssets extends ConsoleCommand
{
protected function configure()
Expand Down
5 changes: 3 additions & 2 deletions plugins/WordPress/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace Piwik\Plugins\WordPress;

use Piwik\Common;
use Piwik\Piwik;
if (!defined( 'ABSPATH')) {
exit; // if accessed directly
}

class Controller extends \Piwik\Plugin\Controller
{
Expand Down
10 changes: 0 additions & 10 deletions plugins/WordPress/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,14 @@

use Exception;
use Piwik\API\Request;
use Piwik\AuthResult;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\FrontController;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\CoreHome\SystemSummary\Item;
use Piwik\Scheduler\Task;
use Piwik\Url;
use Piwik\View;
use Piwik\Widget\WidgetsList;
use WpMatomo\Access;
use WpMatomo\Admin\TrackingSettings;
use WpMatomo\API;
use WpMatomo\Bootstrap;
use WpMatomo\Settings;
use WpMatomo\Site;
use WpMatomo\User;

if (!defined( 'ABSPATH')) {
exit; // if accessed directly
Expand Down

0 comments on commit 52d5bfa

Please sign in to comment.