class-wpml-translation-element-factory.php
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class WPML_Translation_Element_Factory {
const ELEMENT_TYPE_POST = 'Post';
const ELEMENT_TYPE_TERM = 'Term';
const ELEMENT_TYPE_MENU = 'Menu';
/** @var SitePress */
private $sitepress;
/** @var WPML_WP_Cache */
private $wpml_cache;
/**
* @param SitePress $sitepress
* @param WPML_WP_Cache $wpml_cache
*/
public function __construct( SitePress $sitepress, WPML_WP_Cache $wpml_cache = null ) {
$this->sitepress = $sitepress;
$this->wpml_cache = $wpml_cache;
}
/**
* @param int $id
* @param string $type any of `WPML_Translation_Element_Factory::ELEMENT_TYPE_POST`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_TERM`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_MENU`.
*
* @return WPML_Translation_Element
* @throws InvalidArgumentException InvalidArgumentException.
*/
public function create( $id, $type ) {
$fn = 'create_' . $type;
if ( method_exists( $this, $fn ) ) {
return $this->$fn( $id );
}
throw new InvalidArgumentException( 'Element type: ' . $type . ' does not exist.' );
}
public function create_post( $id ) {
return new WPML_Post_Element( $id, $this->sitepress, $this->wpml_cache );
}
public function create_term( $id ) {
return new WPML_Term_Element( $id, $this->sitepress, '', $this->wpml_cache );
}
public function create_menu( $id ) {
return new WPML_Menu_Element( $id, $this->sitepress, $this->wpml_cache );
}
}