Source of file MslsMain.php

Size: 3,793 Bytes - Last Modified: 2018-02-05T14:23:18+00:00

/home/dennisploetner/Projects/Multisite-Language-Switcher/includes/MslsMain.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
<?php
/**
 * MslsMain
 * @author Dennis Ploetner <re@lloc.de>
 * @since 0.9.8
 */

namespace lloc\Msls;

/**
 * Abstraction for the hook classes
 *
 * @package Msls
 */
class MslsMain {

	/**
	 * @var MslsOptions
	 */
	protected $options;

	/**
	 * @var MslsBlogCollection
	 */
	protected $collection;

	/**
	 * @param MslsOptions $options
	 * @param MslsBlogCollection $collection
	 */
	public function __construct( MslsOptions $options, MslsBlogCollection $collection ) {
		$this->options    = $options;
		$this->collection = $collection;
	}

	/**
	 * Factory
	 *
	 * @codeCoverageIgnore
	 *
	 * @return MslsMain
	 */
	public static function init() {
		$options    = MslsOptions::instance();
		$collection = MslsBlogCollection::instance();

		return new static( $options, $collection );
	}

	/**
	 * Prints a message in the error log if WP_DEBUG is true
	 *
	 * @param mixed $message
	 */
	public function debugger( $message ) {
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
			if ( is_array( $message ) || is_object( $message ) ) {
				$message = print_r( $message, true );
			}
			error_log( 'MSLS Debug: ' . $message );
		}
	}

	/**
	 * Get the input array
	 * @param int $object_id
	 * @return array
	 */
	public function get_input_array( $object_id ) {
		$arr = array();

		$current_blog = $this->collection->get_current_blog();
		if ( ! is_null( $current_blog ) ) {
			$arr[ $current_blog->get_language() ] = (int) $object_id;
		}

		$input_post = filter_input_array( INPUT_POST );
		if ( is_array( $input_post ) ) {
			foreach ( $input_post as $key => $value ) {
				if ( false !== strpos( $key, 'msls_input_' ) && ! empty( $value ) ) {
					$arr[ substr( $key, 11 ) ] = (int) $value;
				}
			}
		}

		return $arr;
	}

	/**
	 * Checks if the current input comes from the autosave-functionality
	 * @param int $post_id
	 * @return bool
	 */
	public function is_autosave( $post_id ) {
		return( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id );
	}

	/**
	 * Checks for the nonce in the INPUT_POST
	 * @return boolean
	 */
	public function verify_nonce() {
		return(
			filter_has_var( INPUT_POST, 'msls_noncename' ) &&
			wp_verify_nonce( filter_input( INPUT_POST, 'msls_noncename' ), MSLS_PLUGIN_PATH )
		);
	}

	/**
	 * Delete
	 * @param int $object_id
	 * @codeCoverageIgnore
	 */
	public function delete( $object_id ) {
		$this->save( $object_id, MslsOptionsPost::class );
	}

	/**
	 * Save
	 * @param int $object_id
	 * @param string $class
	 * @codeCoverageIgnore
	 */
	protected function save( $object_id, $class ) {
		if ( has_action( 'msls_main_save' ) ) {
			/**
			 * Calls completely customized save-routine
			 * @since 0.9.9
			 * @param int $object_id
			 * @param string Classname
			 */
			do_action( 'msls_main_save', $object_id, $class );
			return;
		}

		if ( ! $this->collection->has_current_blog() ) {
			$this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' );
			return;
		}

		$language = $this->collection->get_current_blog()->get_language();
		$msla     = new MslsLanguageArray( $this->get_input_array( $object_id ) );
		$options  = new $class( $object_id );
		$temp     = $options->get_arr();

		if ( 0 != $msla->get_val( $language ) ) {
			$options->save( $msla->get_arr( $language ) );
		}
		else {
			$options->delete();
		}

		foreach ( $this->collection->get() as $blog ) {
			switch_to_blog( $blog->userblog_id );

			$language = $blog->get_language();
			$larr_id  = $msla->get_val( $language );

			if ( 0 != $larr_id ) {
				$options = new $class( $larr_id );
				$options->save( $msla->get_arr( $language ) );
			}
			elseif ( isset( $temp[ $language ] ) ) {
				$options = new $class( $temp[ $language ] );
				$options->delete();
			}

			restore_current_blog();
		}
	}

}