| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Riverbank functions file |
|---|
| 4 | * |
|---|
| 5 | * @author Themeisle |
|---|
| 6 | * @package riverbank |
|---|
| 7 | * @since 1.0.0 |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | namespace Riverbank; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * Bootstrap the theme. |
|---|
| 14 | * |
|---|
| 15 | * @return void |
|---|
| 16 | */ |
|---|
| 17 | function bootstrap() { |
|---|
| 18 | global $_riverbank_bootstrap_errors; |
|---|
| 19 | |
|---|
| 20 | $_riverbank_bootstrap_errors = new \WP_Error(); |
|---|
| 21 | |
|---|
| 22 | check_php(); |
|---|
| 23 | |
|---|
| 24 | define_constants(); |
|---|
| 25 | |
|---|
| 26 | check_build_files(); |
|---|
| 27 | |
|---|
| 28 | maybe_add_notices(); |
|---|
| 29 | |
|---|
| 30 | load_sdk(); |
|---|
| 31 | |
|---|
| 32 | load_dependencies(); |
|---|
| 33 | |
|---|
| 34 | run(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Checks that the PHP version is correct. |
|---|
| 39 | * |
|---|
| 40 | * @return void |
|---|
| 41 | */ |
|---|
| 42 | function check_php() { |
|---|
| 43 | global $_riverbank_bootstrap_errors; |
|---|
| 44 | |
|---|
| 45 | if ( version_compare( PHP_VERSION, '7.0' ) > 0 ) { |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | $_riverbank_bootstrap_errors->add( |
|---|
| 50 | 'php_version', |
|---|
| 51 | sprintf( |
|---|
| 52 | /* translators: %s message to upgrade PHP to the latest version */ |
|---|
| 53 | __( "Hey, we've noticed that you're running an outdated version of PHP which is no longer supported. Make sure your site is fast and secure, by %1\$s. Riverbank's minimal requirement is PHP%2\$s.", 'riverbank' ), |
|---|
| 54 | sprintf( |
|---|
| 55 | /* translators: %s message to upgrade PHP to the latest version */ |
|---|
| 56 | '<a href="https://wordpress.org/support/upgrade-php/">%s</a>', |
|---|
| 57 | __( 'upgrading PHP to the latest version', 'riverbank' ) |
|---|
| 58 | ), |
|---|
| 59 | '7.0' |
|---|
| 60 | ) |
|---|
| 61 | ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Define theme constants. |
|---|
| 66 | * |
|---|
| 67 | * @return void |
|---|
| 68 | */ |
|---|
| 69 | function define_constants() { |
|---|
| 70 | define( 'RIVERBANK_VERSION', '1.0.11' ); |
|---|
| 71 | define( 'RIVERBANK_DEBUG', defined( 'WP_DEBUG' ) && WP_DEBUG === true ); |
|---|
| 72 | define( 'RIVERBANK_DIR', trailingslashit( get_template_directory() ) ); |
|---|
| 73 | define( 'RIVERBANK_URL', trailingslashit( get_template_directory_uri() ) ); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Checks that the build files are present. |
|---|
| 78 | * |
|---|
| 79 | * @return void |
|---|
| 80 | */ |
|---|
| 81 | function check_build_files() { |
|---|
| 82 | if ( defined( 'RIVERBANK_IGNORE_SOURCE_CHECK' ) ) { |
|---|
| 83 | return; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | $_files_to_check = array( |
|---|
| 87 | RIVERBANK_DIR . 'vendor/autoload.php', |
|---|
| 88 | RIVERBANK_DIR . 'assets/css/build/style.css', |
|---|
| 89 | RIVERBANK_DIR . 'assets/css/build/editor.css', |
|---|
| 90 | RIVERBANK_DIR . 'assets/css/build/style-rtl.css', |
|---|
| 91 | RIVERBANK_DIR . 'assets/css/build/editor-rtl.css', |
|---|
| 92 | ); |
|---|
| 93 | |
|---|
| 94 | foreach ( $_files_to_check as $file ) { |
|---|
| 95 | if ( is_file( $file ) ) { |
|---|
| 96 | continue; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | global $_riverbank_bootstrap_errors; |
|---|
| 100 | |
|---|
| 101 | $_riverbank_bootstrap_errors->add( |
|---|
| 102 | 'build_missing', |
|---|
| 103 | sprintf( |
|---|
| 104 | /* translators: %s: commands to run the theme */ |
|---|
| 105 | __( 'You appear to be running the Riverbank theme from source code. Please finish installation by running %s.', 'riverbank' ), |
|---|
| 106 | '<code>composer install --no-dev && yarn install --frozen-lockfile && yarn run build</code>' |
|---|
| 107 | ) |
|---|
| 108 | ); |
|---|
| 109 | |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * Adds notices if something went wrong and activates the default theme. |
|---|
| 116 | * |
|---|
| 117 | * @return void |
|---|
| 118 | */ |
|---|
| 119 | function maybe_add_notices() { |
|---|
| 120 | global $_riverbank_bootstrap_errors; |
|---|
| 121 | |
|---|
| 122 | if ( ! $_riverbank_bootstrap_errors->has_errors() ) { |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | add_filter( 'template_include', '__return_null', 99 ); |
|---|
| 127 | switch_theme( WP_DEFAULT_THEME ); |
|---|
| 128 | unset( $_GET['activated'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|---|
| 129 | add_action( |
|---|
| 130 | 'admin_notices', |
|---|
| 131 | function () { |
|---|
| 132 | global $_riverbank_bootstrap_errors; |
|---|
| 133 | |
|---|
| 134 | printf( '<div class="notice notice-error"><p>%1$s</p></div>', wp_kses_post( $_riverbank_bootstrap_errors->get_error_message() ) ); |
|---|
| 135 | } |
|---|
| 136 | ); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Load SDK. |
|---|
| 141 | * |
|---|
| 142 | * @return void |
|---|
| 143 | */ |
|---|
| 144 | function load_sdk() { |
|---|
| 145 | add_filter( |
|---|
| 146 | 'themeisle_sdk_products', |
|---|
| 147 | function ( $products ) { |
|---|
| 148 | $products[] = RIVERBANK_DIR . 'style.css'; |
|---|
| 149 | |
|---|
| 150 | return $products; |
|---|
| 151 | } |
|---|
| 152 | ); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Load composer dependencies. |
|---|
| 157 | * |
|---|
| 158 | * @return void |
|---|
| 159 | */ |
|---|
| 160 | function load_dependencies() { |
|---|
| 161 | $vendor_file = RIVERBANK_DIR . '/vendor/autoload.php'; |
|---|
| 162 | if ( is_readable( $vendor_file ) ) { |
|---|
| 163 | require_once $vendor_file; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Run theme core. |
|---|
| 169 | * |
|---|
| 170 | * @return void |
|---|
| 171 | */ |
|---|
| 172 | function run() { |
|---|
| 173 | Core::get_instance(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | bootstrap(); |
|---|