HOME


Mini Shell 1.0
DIR: /opt/clwpos/
Upload File :
Current File : //opt/clwpos/wp-cli-wrapped
#!/bin/bash
#######################################
# Timed-out wrapper around wp-cli
# Arguments:
#   $1 - Path to PHP binary, full filesystem path
#   $2 - Path to Wordpress installation, full filesystem path
#   All the rest arguments ($@) are treated as WP-CLI command
# Outputs:
#   Writes result to stdout
#######################################
PATH_TO_PHP="$1"
PATH_TO_WP="$2"

shift 2;

# WPOS wp-cli
WP_CLI="/opt/clwpos/wp-cli"

# Defaults for PHP
PHP_DEFAULT_MEMORY_LIMIT="-d memory_limit=-1"
PHP_DEFAULT_EXTENSIONS="phar json mysqli"
# explicitly drop PHP disable_functions directive in order to avoid errors like
# 'Error: Cannot do 'launch': The PHP functions `proc_open()` and/or `proc_close()` are disabled'
# during plugin manipulations
PHP_DEFAULT_FUNCTIONS="-d disable_functions="
# suppress E_WARNING E_NOTICE E_USER_WARNING E_USER_NOTICE E_DEPRECATED E_USER_DEPRECATED warnings from php
PHP_SUPPRESS_ERRORS="-d error_reporting=22527"

# Defaults for WP-CLI
WP_CLI_DEFAULT_OPTS=("--skip-themes")

# passed via ENVVAR
if [ -n "${SKIP_PLUGINS_LOADING}" ]; then
    WP_CLI_DEFAULT_OPTS+=("--skip-plugins")
fi

WP_CRON_DISABLED=$(grep -i "define( *'DISABLE_WP_CRON', *true *);" "${PATH_TO_WP}/wp-config.php")

# If not found, check in the parent directory
if [ -z "$WP_CRON_DISABLED" ]; then
    PARENT_PATH=$(dirname "${PATH_TO_WP}")
    WP_CRON_DISABLED=$(grep -i "define( *'DISABLE_WP_CRON', *true *);" "${PARENT_PATH}/wp-config.php")
fi

if ! [[ $WP_CRON_DISABLED ]]; then
    WP_CLI_DEFAULT_OPTS+=("--exec=define('DISABLE_WP_CRON', true);")
fi

# Default timeout, formatted as for timeout command (GNU coreutils)
# 2 minutes (120 seconds)
TIMEOUT="2m"

# Construct PHP extensions to include
EXTS=""
for ext in $PHP_DEFAULT_EXTENSIONS
do
  if ! $PATH_TO_PHP -m | grep -i "$ext" 1>/dev/null; then
    EXTS+=" -d extension=$ext.so"
  fi
done

# change current working directory
# we need this because if php code has relative imports
# it looks for scripts inside current directory first
# e.g. some providers add require('wp-salt.php') to config
# https://stackoverflow.com/questions/75823716/
# the overall approach of relative import is not correct,
# but we still need patch for this
cd "${PATH_TO_WP}"
if [[ "$1" == "help" ]]; then
  exec timeout $TIMEOUT $PATH_TO_PHP \
    $EXTS $PHP_SUPPRESS_ERRORS $PHP_DEFAULT_MEMORY_LIMIT $PHP_DEFAULT_FUNCTIONS \
    $WP_CLI --path=$PATH_TO_WP "${WP_CLI_DEFAULT_OPTS[@]}" "$@" | cat
else
  exec timeout $TIMEOUT $PATH_TO_PHP \
    $EXTS $PHP_SUPPRESS_ERRORS $PHP_DEFAULT_MEMORY_LIMIT $PHP_DEFAULT_FUNCTIONS \
    $WP_CLI --path=$PATH_TO_WP "${WP_CLI_DEFAULT_OPTS[@]}" "$@"
fi