| 1 |
<?php |
| 2 |
/** |
| 3 |
* Application/Plugin specific functions. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare( strict_types=1 ); |
| 7 |
|
| 8 |
use SolidWP\Performance\Container; |
| 9 |
use SolidWP\Performance\Core; |
| 10 |
use SolidWP\Performance\lucatume\DI52\Container as DI52Container; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize the plugin singleton and ensure we only have a single |
| 18 |
* instance of our container. |
| 19 |
* |
| 20 |
* @return Core |
| 21 |
*/ |
| 22 |
function swpsp_plugin(): Core { |
| 23 |
// In the event we don't have a container in our core class yet. |
| 24 |
$container = new Container( new DI52Container() ); |
| 25 |
|
| 26 |
// This singleton will not use a new container if one is already set. |
| 27 |
return Core::instance( realpath( __DIR__ . '/../../solid-performance.php' ), $container ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Log to the error log if WP_DEBUG is set to true. |
| 32 |
* |
| 33 |
* @param string|mixed[] $data The data to log. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
function swpsp_log( $data ): void { |
| 38 |
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG || ! function_exists( 'error_log' ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if ( is_array( $data ) ) { |
| 43 |
$data = print_r( $data, true ); |
| 44 |
} |
| 45 |
|
| 46 |
error_log( $data ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Properly get the home path, as WordPress's function doesn't seem to work in all cases. |
| 51 |
* |
| 52 |
* This is based off the same code that wp-load.php uses to find wp-config.php. |
| 53 |
* |
| 54 |
* @throws RuntimeException If we can't find the home path. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
function swpsp_get_document_root(): string { |
| 59 |
$path = ''; |
| 60 |
|
| 61 |
if ( file_exists( ABSPATH . 'wp-config.php' ) ) { |
| 62 |
$path = ABSPATH; |
| 63 |
} elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { |
| 64 |
$path = dirname( ABSPATH ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! $path ) { |
| 68 |
throw new RuntimeException( 'Unable to document root' ); |
| 69 |
} |
| 70 |
|
| 71 |
return trailingslashit( $path ); |
| 72 |
} |
| 73 |
|