Source of file MslsAdminIcon.php

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

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

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

namespace lloc\Msls;

/**
 * Handles the icon links in the backend
 * @package Msls
 */
class MslsAdminIcon {

	/**
	 * Language
	 * @var string
	 */
	protected $language;

	/**
	 * Origin Language
	 * @var string
	 */
	public $origin_language;

	/**
	 * Source
	 * @var string
	 */
	protected $src;

	/**
	 * URL
	 * @var string
	 */
	protected $href;

	/**
	 * Blog id
	 * @var int
	 */
	protected $blog_id;

	/**
	 * Type
	 * @var string
	 */
	protected $type;

	/**
	 * Path
	 * @var string
	 */
	protected $path = 'post-new.php';

	/**
	 * The current object ID
	 * @var int
	 */
	protected $id;

	/**
	 * Factory method
	 * @return MslsAdminIcon
	 */
	public static function create() {
		$obj  = MslsContentTypes::create();
		$type = $obj->get_request();

		if ( $obj->is_taxonomy() ) {
			return new MslsAdminIconTaxonomy( $type );
		}

		return new MslsAdminIcon( $type );
	}

	/**
	 * Constructor
	 * @param string $type
	 */
	public function __construct( $type ) {
		$this->type = esc_attr( $type );
		$this->set_path();
	}

	/**
	 * Set the path by type
	 *
	 * @uses add_query_arg()
	 *
	 * @return MslsAdminIcon
	 */
	public function set_path() {
		if ( 'post' != $this->type ) {
			$this->path = add_query_arg( [ 'post_type' => $this->type ], $this->path );
		}

		return $this;
	}

	/**
	 * Set language
	 *
	 * @param string $language
	 *
	 * @return MslsAdminIcon
	 */
	public function set_language( $language ) {
		$this->language = $language;

		return $this;
	}

	/**
	 * Set src
	 *
	 * @param string $src
	 *
	 * @return MslsAdminIcon
	 */
	public function set_src( $src ) {
		$this->src = $src;

		return $this;
	}

	/**
	 * Set href
	 * @uses get_edit_post_link()
	 *
	 * @param int $id
	 *
	 * @return MslsAdminIcon
	 */
	public function set_href( $id ) {
		$this->href = get_edit_post_link( $id );

		return $this;
	}

	/**
	 * Handles the output when object is treated like a string
	 * @return string
	 */
	public function __toString() {
		return $this->get_a();
	}

	/**
	 * Get image as html-tag
	 *
	 * @return string
	 */
	public function get_img() {
		return sprintf( '<img alt="%s" src="%s" />', $this->language, $this->src );
	}

	/**
	 * Get link as html-tag
	 * @return string
	 */
	public function get_a() {
		if ( ! empty( $this->href ) ) {
			$href  = $this->href;
			$title = sprintf(
				__( 'Edit the translation in the %s-blog', 'multisite-language-switcher' ),
				$this->language
			);
		}
		else {
			$href  = $this->get_edit_new();
			$title = sprintf(
				__( 'Create a new translation in the %s-blog', 'multisite-language-switcher' ),
				$this->language
			);
		}
		return sprintf(
			'<a title="%s" href="%s">%s</a>&nbsp;',
			$title,
			$href,
			$this->get_img()
		);
	}

	/**
	 * Creates new admin link
	 * @uses get_admin_url()
	 * @todo check if we need this method separately
	 * @return string
	 */
	public function get_edit_new() {
		$path = $this->path;

		if ( null !== $this->id && null !== $this->origin_language ) {
			$path = add_query_arg(
				array( 'msls_id' => $this->id, 'msls_lang' => $this->origin_language ),
				$this->path
			);
		}

		/**
		 * Returns custom url of an admin icon link
		 * @since 0.9.9
		 * @param string $path
		 */
		return get_admin_url(
			get_current_blog_id(),
			(string) apply_filters( 'msls_admin_icon_get_edit_new', $path )
		);
	}

	/**
	 * Sets the id of the object this icon is for
	 *
	 * @param int $id
	 *
	 * @return MslsAdminIcon
	 */
	public function set_id( $id ) {
		$this->id = $id;

		return $this;
	}

	/**
	 * Sets the origin language for this icon
	 *
	 * @param string $origin_language
	 *
	 * @return MslsAdminIcon
	 */
	public function set_origin_language( $origin_language ) {
		$this->origin_language = $origin_language;

		return $this;
	}

}