1 <?php
2 /**
3 * Admin Page Framework
4 *
5 * http://en.michaeluno.jp/admin-page-framework/
6 * Copyright (c) 2013-2014 Michael Uno; Licensed MIT
7 *
8 */
9 if ( ! class_exists( 'AdminPageFramework_PostType_Controller' ) ) :
10 /**
11 * Provides methods of views for the post type factory class.
12 *
13 * Those methods are public and provides means for users to set property values.
14 *
15 * @abstract
16 * @since 3.0.4
17 * @package AdminPageFramework
18 * @subpackage PostType
19 */
20 abstract class AdminPageFramework_PostType_Controller extends AdminPageFramework_PostType_View {
21
22 function __construct( $oProp ) {
23
24 parent::__construct( $oProp );
25
26 if ( $this->_isInThePage() ) :
27
28 add_action( 'wp_loaded', array( $this, 'setup_pre' ) );
29
30 endif;
31
32 }
33
34 /**
35 * The method for all necessary set-ups.
36 *
37 * <h4>Example</h4>
38 * <code>public function setUp() {
39 * $this->setAutoSave( false );
40 * $this->setAuthorTableFilter( true );
41 * $this->addTaxonomy(
42 * 'sample_taxonomy', // taxonomy slug
43 * array( // argument - for the argument array keys, refer to : http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments
44 * 'labels' => array(
45 * 'name' => 'Genre',
46 * 'add_new_item' => 'Add New Genre',
47 * 'new_item_name' => "New Genre"
48 * ),
49 * 'show_ui' => true,
50 * 'show_tagcloud' => false,
51 * 'hierarchical' => true,
52 * 'show_admin_column' => true,
53 * 'show_in_nav_menus' => true,
54 * 'show_table_filter' => true, // framework specific key
55 * 'show_in_sidebar_menus' => false, // framework specific key
56 * )
57 * );
58 * }</code>
59 *
60 * @abstract
61 * @since 2.0.0
62 * @remark The user should override this method in their class definition.
63 * @remark A callback for the <em>wp_loaded</em> hook.
64 */
65 public function setUp() {}
66
67 /*
68 * Head Tag Methods
69 */
70 /**
71 * Enqueues styles by page slug and tab slug.
72 *
73 * @since 3.0.0
74 * @return array An array holding the handle IDs of queued items.
75 */
76 public function enqueueStyles( $aSRCs, $aCustomArgs=array() ) {
77 if ( method_exists( $this->oHeadTag, '_enqueueStyles' ) ) {
78 return $this->oHeadTag->_enqueueStyles( $aSRCs, array( $this->oProp->sPostType ), $aCustomArgs );
79 }
80 }
81 /**
82 * Enqueues a style by page slug and tab slug.
83 *
84 * @since 3.0.0
85 * @see http://codex.wordpress.org/Function_Reference/wp_enqueue_style
86 * @param string The URL of the stylesheet to enqueue, the absolute file path, or the relative path to the root directory of WordPress. Example: '/css/mystyle.css'.
87 * @param array (optional) The argument array for more advanced parameters.
88 * <h4>Custom Argument Array</h4>
89 * <ul>
90 * <li><strong>handle_id</strong> - ( optional, string ) The handle ID of the stylesheet.</li>
91 * <li><strong>dependencies</strong> - ( optional, array ) The dependency array. For more information, see <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style">codex</a>.</li>
92 * <li><strong>version</strong> - ( optional, string ) The stylesheet version number.</li>
93 * <li><strong>media</strong> - ( optional, string ) the description of the field which is inserted into after the input field tag.</li>
94 * </ul>
95 * @return string The script handle ID. If the passed url is not a valid url string, an empty string will be returned.
96 */
97 public function enqueueStyle( $sSRC, $aCustomArgs=array() ) {
98 if ( method_exists( $this->oHeadTag, '_enqueueStyle' ) ) {
99 return $this->oHeadTag->_enqueueStyle( $sSRC, array( $this->oProp->sPostType ), $aCustomArgs );
100 }
101 }
102 /**
103 * Enqueues scripts by page slug and tab slug.
104 *
105 * @since 3.0.0
106 * @return array An array holding the handle IDs of queued items.
107 */
108 public function enqueueScripts( $aSRCs, $aCustomArgs=array() ) {
109 if ( method_exists( $this->oHeadTag, '_enqueueScripts' ) ) {
110 return $this->oHeadTag->_enqueueScripts( $aSRCs, array( $this->oProp->sPostType ), $aCustomArgs );
111 }
112 }
113 /**
114 * Enqueues a script by page slug and tab slug.
115 *
116 * <h4>Example</h4>
117 * <code>$this->enqueueScript(
118 * plugins_url( 'asset/js/test.js' , __FILE__ ), // source url or path
119 * array(
120 * 'handle_id' => 'my_script', // this handle ID also is used as the object name for the translation array below.
121 * 'translation' => array(
122 * 'a' => 'hello world!',
123 * 'style_handle_id' => $sStyleHandle, // check the enqueued style handle ID here.
124 * ),
125 * )
126 * );</code>
127 *
128 * @since 3.0.0
129 * @see http://codex.wordpress.org/Function_Reference/wp_enqueue_script
130 * @param string The URL of the stylesheet to enqueue, the absolute file path, or the relative path to the root directory of WordPress. Example: '/js/myscript.js'.
131 * @param array (optional) The argument array for more advanced parameters.
132 * <h4>Custom Argument Array</h4>
133 * <ul>
134 * <li><strong>handle_id</strong> - ( optional, string ) The handle ID of the script.</li>
135 * <li><strong>dependencies</strong> - ( optional, array ) The dependency array. For more information, see <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">codex</a>.</li>
136 * <li><strong>version</strong> - ( optional, string ) The stylesheet version number.</li>
137 * <li><strong>translation</strong> - ( optional, array ) The translation array. The handle ID will be used for the object name.</li>
138 * <li><strong>in_footer</strong> - ( optional, boolean ) Whether to enqueue the script before <code></head ></code> or before <code></body></code> Default: <em>false</em>.</li>
139 * </ul>
140 * @return string The script handle ID. If the passed url is not a valid url string, an empty string will be returned.
141 */
142 public function enqueueScript( $sSRC, $aCustomArgs=array() ) {
143 if ( method_exists( $this->oHeadTag, '_enqueueScript' ) ) {
144 return $this->oHeadTag->_enqueueScript( $sSRC, array( $this->oProp->sPostType ), $aCustomArgs );
145 }
146 }
147
148 /*
149 * Front-end methods
150 */
151 /**
152 * Enables or disables the auto-save feature in the custom post type's post submission page.
153 *
154 * <h4>Example</h4>
155 * <code>$this->setAutoSave( false );
156 * </code>
157 *
158 * @since 2.0.0
159 * @param boolean If true, it enables the auto-save; otherwise, it disables it.
160 * return void
161 */
162 protected function setAutoSave( $bEnableAutoSave=True ) {
163 $this->oProp->bEnableAutoSave = $bEnableAutoSave;
164 }
165
166 /**
167 * Adds a custom taxonomy to the class post type.
168 * <h4>Example</h4>
169 * <code>$this->addTaxonomy(
170 * 'sample_taxonomy', // taxonomy slug
171 * array( // argument
172 * 'labels' => array(
173 * 'name' => 'Genre',
174 * 'add_new_item' => 'Add New Genre',
175 * 'new_item_name' => "New Genre"
176 * ),
177 * 'show_ui' => true,
178 * 'show_tagcloud' => false,
179 * 'hierarchical' => true,
180 * 'show_admin_column' => true,
181 * 'show_in_nav_menus' => true,
182 * 'show_table_filter' => true, // framework specific key
183 * 'show_in_sidebar_menus' => false, // framework specific key
184 * )
185 * );</code>
186 *
187 * @see http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments
188 * @since 2.0.0
189 * @since 3.1.1 Added the third parameter.
190 * @param string $sTaxonomySlug The taxonomy slug.
191 * @param array $aArgs The taxonomy argument array passed to the second parameter of the <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments">register_taxonomy()</a> function.
192 * @param array $aAdditionalObjectTypes Additional object types(post types) besides the caller post type.
193 * @return void
194 */
195 protected function addTaxonomy( $sTaxonomySlug, array $aArgs, array $aAdditionalObjectTypes=array() ) {
196
197 $sTaxonomySlug = $this->oUtil->sanitizeSlug( $sTaxonomySlug );
198 $this->oProp->aTaxonomies[ $sTaxonomySlug ] = $aArgs;
199 if ( isset( $aArgs['show_table_filter'] ) && $aArgs['show_table_filter'] ) {
200 $this->oProp->aTaxonomyTableFilters[] = $sTaxonomySlug;
201 }
202 if ( isset( $aArgs['show_in_sidebar_menus'] ) && ! $aArgs['show_in_sidebar_menus'] ) {
203 $this->oProp->aTaxonomyRemoveSubmenuPages[ "edit-tags.php?taxonomy={$sTaxonomySlug}&post_type={$this->oProp->sPostType}" ] = "edit.php?post_type={$this->oProp->sPostType}";
204 }
205 if ( count( $this->oProp->aTaxonomyTableFilters ) == 1 ) {
206 add_action( 'init', array( $this, '_replyToRegisterTaxonomies' ) ); // the hook should not be admin_init because taxonomies need to be accessed in regular pages.
207 }
208 if ( count( $this->oProp->aTaxonomyRemoveSubmenuPages ) == 1 ) {
209 add_action( 'admin_menu', array( $this, '_replyToRemoveTexonomySubmenuPages' ), 999 );
210 }
211
212 $_aExistingObjectTypes = isset( $this->oProp->aTaxonomyObjectTypes[ $sTaxonomySlug ] ) && is_array( $this->oProp->aTaxonomyObjectTypes[ $sTaxonomySlug ] )
213 ? $this->oProp->aTaxonomyObjectTypes[ $sTaxonomySlug ]
214 : array();
215 $aAdditionalObjectTypes = array_merge( $_aExistingObjectTypes, $aAdditionalObjectTypes );
216 $this->oProp->aTaxonomyObjectTypes[ $sTaxonomySlug ] = array_unique( $aAdditionalObjectTypes );
217
218 }
219
220 /**
221 * Sets whether the author drop-down filter is enabled/disabled in the post type post list table.
222 *
223 * <h4>Example</h4>
224 * <code>$this->setAuthorTableFilter( true );
225 * </code>
226 *
227 * @since 2.0.0
228 * @param boolean If true, it enables the author filter; otherwise, it disables it.
229 * @return void
230 */
231 protected function setAuthorTableFilter( $bEnableAuthorTableFileter=false ) {
232 $this->oProp->bEnableAuthorTableFileter = $bEnableAuthorTableFileter;
233 }
234
235 /**
236 * Sets the post type arguments.
237 *
238 * This is only necessary if it is not set in the constructor.
239 *
240 * @since 2.0.0
241 * @see http://codex.wordpress.org/Function_Reference/register_post_type#Arguments
242 * @param array The <a href="http://codex.wordpress.org/Function_Reference/register_post_type#Arguments">array of arguments</a> to be passed to the second parameter of the <em>register_post_type()</em> function.
243 * @return void
244 */
245 protected function setPostTypeArgs( $aArgs ) {
246 $this->oProp->aPostTypeArgs = $aArgs;
247 }
248
249 /**
250 * Sets the given HTML text into the footer on the left hand side.
251 *
252 * <h4>Example</h4>
253 * <code>$this->setFooterInfoLeft( '<br />Custom Text on the left hand side.' );
254 * </code>
255 *
256 * @since 2.0.0
257 * @param string The HTML code to insert.
258 * @param boolean If true, the text will be appended; otherwise, it will replace the default text.
259 * @return void
260 */
261 protected function setFooterInfoLeft( $sHTML, $bAppend=true ) {
262 if ( isset( $this->oLink ) ) // check if the object is set to ensure it won't trigger a warning message in non-admin pages.
263 $this->oLink->aFooterInfo['sLeft'] = $bAppend
264 ? $this->oLink->aFooterInfo['sLeft'] . $sHTML
265 : $sHTML;
266 }
267
268 /**
269 * Sets the given HTML text into the footer on the right hand side.
270 *
271 * <h4>Example</h4>
272 * <code>$this->setFooterInfoRight( '<br />Custom Text on the right hand side.' );
273 * </code>
274 *
275 * @since 2.0.0
276 * @param string The HTML code to insert.
277 * @param boolean If true, the text will be appended; otherwise, it will replace the default text.
278 * @return void
279 */
280 protected function setFooterInfoRight( $sHTML, $bAppend=true ) {
281 if ( isset( $this->oLink ) ) // check if the object is set to ensure it won't trigger a warning message in non-admin pages.
282 $this->oLink->aFooterInfo['sRight'] = $bAppend
283 ? $this->oLink->aFooterInfo['sRight'] . $sHTML
284 : $sHTML;
285 }
286
287
288 }
289 endif;