-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheAppFactory.class.php
1627 lines (1472 loc) · 53.9 KB
/
TheAppFactory.class.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class TheAppFactory {
/**
* Static property to hold our singleton instance
*/
static $instance = false;
/**
* Variable to hold a reference to the instances we've setup
*/
var $instances_setup = array();
/**
* Variable to hold a reference to the post object
*/
var $post;
/**
* Variable to hold a reference to the parameters object
*/
var $parms;
/**
* Variable to hold the model, view, controller, etc queues
*/
var $queued;
/**
* Variable to hold the registered models, views, controllers, etc
*/
var $registered;
/**
* This is our constructor, which is private to force the use of
* getInstance() to make this a Singleton
*
* @return TheAppFactory
*/
private function __construct() {
$this->init();
// Fires only if in WordPress Admin Area, do some actions
if (is_admin()){
//add_action('init',array(&$this,'wp_init));
}
// Other actions to always perform.
//add_action('template_redirect',array(&$this,'template_redirect'));
}
/**
* If an instance exists, this returns it. If not, it creates one and
* retuns it.
*
* @return TheAppFactory
*/
public static function getInstance( $class_name = 'TheAppFactory' ) {
if ( !self::$instance ) {
self::instantiate( $class_name );
}
if (!in_array($class_name,self::$instance->instances_setup)){
call_user_func( array( $class_name, 'setup_environment') );
self::$instance->instances_setup[] = $class_name;
}
return self::$instance;
}
/**
* Simply instantiates the singleton
*
* @return void
*/
public function instantiate( $class_name = 'TheAppFactory' ) {
if ( !self::$instance ) {
self::$instance = new $class_name;
self::$instance->setup();
self::$instance->setup_post();
}
return void;
}
private function init(){
$this->reset();
add_shortcode('the_app',array(&$this,'shortcodes'));
add_shortcode('app_item',array(&$this,'shortcodes'));
add_shortcode('app_item_wrapper',array(&$this,'shortcodes'));
add_shortcode('app_posts',array(&$this,'shortcodes'));
add_shortcode('loading_spinner',array(&$this,'shortcodes'));
add_shortcode('unacceptable_browser',array(&$this,'shortcodes'));
add_shortcode('launch',array(&$this,'shortcodes'));
add_shortcode('launch_item',array(&$this,'shortcodes'));
add_filter('TheAppFactory_models',array(&$this,'addRegisteredModels'),10,2);
add_filter('TheAppFactory_stores',array(&$this,'addRegisteredStores'),10,2);
add_filter('TheAppFactory_helpers',array(&$this,'addLaunchHelper'),10,2);
add_action('TheAppFactory_setupStores',array(&$this, 'setupStoreStatusStore'),500); // Make sure the Store Status is setup last so as to allow the other stores to instantiate first
add_action('TheAppFactory_setupStores',array(&$this, 'massageStoreConfigs'),600);
add_action('TheAppFactory_parsePost', array(&$this, 'maybeSetupSheetMenuItems' ), 100 );
do_action_ref_array('TheAppFactory_init',array(& $this));
}
// Override in subclasses
public function setup(){
self::set_environment();
}
// Override in subclasses
public static function setup_environment(){
}
public function set_environment(){
$the_app = self::getInstance();
$app_meta = get_post_meta(get_the_ID(),'app_meta',true);
$key = (current_user_can('administrator') ? 'admin' : 'regular');
if ($app_meta and isset($app_meta['visibility']) and isset($app_meta['visibility'][$key])){
$the_app->set('environment', $app_meta['visibility'][$key]); // i.e. 'development', 'production', 'native_ios', 'native_android'
}
else{
$the_app->set('environment', 'development');
}
}
public function save_postdata( $post_id ){
/* When the post is saved, saves our custom data */
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['app_meta_nonce'], plugin_basename( __FILE__ ) ) )
return;
$post = get_post($post_id);
//skip all cases where we shouldn't index
if ( $post->post_type != APP_POST_TYPE )
return;
if (!empty($_POST['app_meta']['bundle_id'])){
$_POST['app_meta']['bundle_id'] = trim($_POST['app_meta']['bundle_id'],'.');
}
if (isset($_POST['app_meta'])){
update_post_meta($post_id,'app_meta',$_POST['app_meta']);
}
else{
delete_post_meta($post_id,'app_meta');
}
}
private function reset(){
$this->parms = array();
$this->set('transition','slide');
$this->set('items',array());
$this->set('meta',array('unacceptable_browser' => array('not_webkit' => true)));
$this->set('registered_post_queries',array());
$this->set('query_defaults',array(
'author' => '',
'author_name' => '',
'cat' => '',
'category_name' => '',
'category__and' => '',
'category__in' => '',
'category__not_in' => '',
'tag' => '',
'tag_id' => '',
'tag__and' => '',
'tag__in' => '',
'tag__not_in' => '',
'tag_slug__and' => '',
'tag_slug__in' => '',
'p' => '',
'name' => '',
'numberposts' => -1,
'page_id' => '',
'pagename' => '',
'post_parent' => '',
'post__in' => '',
'post__not_in' => '',
'post_type' => 'post',
'post_status' => '',
'order' => 'ASC',
'orderby' => 'title',
'year' => '',
'monthnum' => '',
'w' => '',
'day' => '',
'hour' => '',
'minute' => '',
'second' => '',
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'data_callback' => null,
'model_callback' => null,
'timestamp_callback' => null,
'xtype' => 'itemlist'
));
}
public function get($what=null){
if ($what != null){
return $this->parms[$what];
}
else{
return $this->parms;
}
}
public function set($what,$value){
$this->parms[$what] = $value;
}
public function setup_post( $post = null ){
if (!isset($post)){
global $post;
}
// reset the app
//$this->reset();
// the post is the object that defines the app
// though this is setup to work with WP posts as default
// I'm keeping it open to allow other ways to instantiate
// an app
$this->set('post',$post);
if (isset($post->ID)){
$this->is('wordpress_post',true);
}
else{
$this->is('wordpress_post',false);
}
if ($this->is('wordpress_post')){
// Let's see if there are any custom images to use for the app (startups and icon)
$p = $this->get('post');
$attachments = get_children( array( 'post_parent' => $p->ID, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
$app_attachments_types = apply_filters('TheAppFactory_attachments_types',array('startup_phone','startup_tablet','startup_landscape_tablet','icon','stylesheet','splash'),array(& $this));
foreach ($app_attachments_types as $type){
$guid = null;
if (is_array($attachments) and count($attachments)){
foreach ($attachments as $attachment){
if (strpos($attachment->post_title,$type) === 0 and !$this->get($type)){
// We're going to deal with splashes a little differently. We're
// going to end up base64_encoding them and including them
// in the index file. So, we're going to save the filepath instead
if ($type == 'splash'){
add_filter('the_app_factory_body_style',array(&$this,'addSplashImage'));
$splash_file = get_attached_file($attachment->ID);
}
$guid = $attachment->guid;
}
}
}
$guid = apply_filters('TheAppFactory_attachment_guid',$guid,$type,array(&$this));
if ($guid){
$this->set($type,$guid);
if ( isset( $splash_file ) ){
$this->set('has_splash',true);
$this->set( 'splash_file', $splash_file );
unset( $splash_file );
}
}
}
}
// This is the mothership, where the app calls to get data
$this->set('mothership',apply_filters('TheAppFactory_mothership',trailingslashit(get_permalink()),array(&$this)));
$this->set('app_id',substr(md5($this->get('mothership')),0,5)); // a unique app_id
// Register xtypes for Sencha 2 apps
$this->setupSencha();
// second, setup the parms based on the shortcodes within the post
$this->parsePost();
$this->setupModels();
$this->setupStores();
$this->setupHelpers();
$this->setupProfiles();
}
public function is($what,$value = null){
if (isset($value)){
$this->set('_is_'.$what,$value);
}
return $this->get('_is_'.$what);
}
public function parsePost(){
$post = $this->get('post');
if ($this->is('wordpress_post')){
$this->set('title',$post->post_title);
do_shortcode($post->post_content);
}
do_action_ref_array('TheAppFactory_parsePost',array(& $this));
}
public function apply($what,$value){
$current = $this->get($what);
if (empty($current)){
$current = array();
}
$this->set($what,$this->array_merge_recursive($current,$value));
return $this->get($what);
}
public function remove($value,$what){
if (isset($this->parms[$what]) and array_key_exists($value,$this->parms[$what])){
unset($this->parms[$what][$value]);
}
}
public function shortcode_atts( $defaults, $atts ){
// This is exactly the same as the WP function shortcode_atts
// except that it reCamelCases $atts keys (so if you enter
// thisAttributeName in your shortcode, it actually survives as CamelCased, as opposed to
// being lowercased)
foreach ( array_keys($defaults) as $key ){
$lower_key = strtolower( $key );
if ( isset( $atts[ $lower_key ] ) ){
$atts[ $key ] = $atts[ $lower_key ];
unset( $atts[ $lower_key ] );
}
}
return shortcode_atts( $defaults, $atts );
}
// Exactly the same as the PHP array_merge_recursize, unless it's not an associative
// array (i.e. numeric indexes), in which case, it just fully replaces the previous
// with the next's value.
//
// Adapted from @walf's comment at http://www.php.net/manual/en/function.array-merge-recursive.php
public function array_merge_recursive() {
if (func_num_args() < 2) {
trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING);
return;
}
$arrays = func_get_args();
$merged = array();
while ($arrays) {
$array = array_shift($arrays);
if (!is_array($array)) {
trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
return;
}
if (!$array)
continue;
if ($this->is_assoc($array)){
foreach ($array as $key => $value)
if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
$merged[$key] = call_user_func(array(&$this,'array_merge_recursive'), $merged[$key], $value);
else
$merged[$key] = $value;
}
else{
$merged = $array;
}
}
return $merged;
}
// Thanks http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-numeric
public function is_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
public function shortcodes($atts = array(),$content = null,$code = ''){
if (!is_array($atts)){
$atts = array();
}
$original_atts = $atts;
$atts = $this->sanitize_atts($atts,$code); // calls @shortcode_atts
switch($code){
case 'the_app':
foreach ($atts as $key => $att){
if (substr($key,0,4) == '_is_'){
$this->set($key,($att ? true : false));
}
else{
$this->set($key,$att);
}
}
if ( $this->is( 'using_manifest' ) ){
switch( $this->get( 'storage_engine' ) ){
case 'localstorage':
// No need to do anything,
break;
case 'sqlitestorage':
$this->enqueue( 'require', 'SqliteDemo.util.InitSQLite' );
break;
}
}
// Make sure we're always working with the latest Sencha Touch SDK
$defaults = $this->get_default_atts( $code );
$this->set( 'sdk', $defaults['sdk'] );
break;
case 'launch':
$atts['slide_pause'] = intval( $atts['slide_pause'] );
$this->set( 'launchConfig', $atts );
do_shortcode( $content );
break;
case 'launch_item':
$launch_items = $this->get( 'launchItems' );
if ( !is_array( $launch_items ) ){
$launch_items = array();
}
if ( isset( $atts['slide_pause'] ) ){
$atts['slide_pause'] = intval( $atts['slide_pause'] );
}
$launch_items[] = array_filter($atts);
$this->set( 'launchItems', $launch_items );
break;
case 'loading_spinner':
foreach ($atts as $key => $att){
$this->set('spinner-'.$key,$att);
}
break;
case 'unacceptable_browser':
$unacceptable_browser = array();
foreach ($atts as $key => $att){
$unacceptable_browser[$key] = $att;
}
$unacceptable_browser['content'] = isset( $content ) ? $content : __('Your browser is not supported. Please use a Webkit Browser (i.e. Chrome, Safari, iPhone, Android).','app-factory');
$meta = $this->get('meta');
$meta['unacceptable_browser'] = $unacceptable_browser;
$this->set('meta',$meta);
break;
case 'app_item_wrapper':
$this->is('capturing',true);
$this->set('captured_items',array());
do_shortcode($content);
$this->is('capturing',false);
$atts['pages'] = $this->get('captured_items');
$this->addWrapperItem($atts);
break;
case 'app_item':
switch(true){
case (isset($atts['callback']) and is_callable($atts['callback'])):
$callback = $atts['callback'];
call_user_func_array($atts['callback'],array($original_atts)); // pass the original, so there's no filtering.
break;
case (isset($atts['post_type'])):
$this->addPostListItem($atts);
break;
case (isset($atts['post']) and is_numeric($atts['post'])):
$p = get_post($atts['post']);
if ($p){
if (empty($atts['title'])) $atts['title'] = $p->post_title;
$atts['content'] = apply_filters('the_content',$p->post_content);
$this->addHTMLItem($atts);
}
break;
case (isset($content) and trim($content) != ''):
$atts['content'] = apply_filters('the_content',$content);
$this->addHTMLItem($atts);
break;
}
break;
case 'app_posts':
if ($atts['orderby'] == 'date'){
// 'date' is formatted as "Jan 10 2011", which doesn't sort as well as 2011-01-10
$atts['orderby'] = 'date_gmt';
}
$this->addPostListItem($atts);
break;
}
}
function sanitize_atts($atts = array(),$shortcode = null){
// Sanitize boolean strings to actual booleans
if ( !is_array( $atts ) ){
$atts = array();
}
foreach ( $atts as $key => $value ){
if ( $value === 'false' ){
$atts[ $key ] = false;
}
elseif ( $value === 'true' ){
$atts[ $key ] = true;
}
// Add in a camelCaseKey for any camel_case_key attributes;
if ( strpos( $key, '_' ) !== false ) {
$pieces = array_map( 'ucfirst', explode( '_', $key ) );
$pieces[0] = strtolower( $pieces[0] );
$camelCaseKey = implode( '', $pieces);
$atts[ $camelCaseKey ] = $atts[ $key ];
}
}
if ( isset($shortcode) ){
$defaults = $this->get_default_atts($shortcode);
}
if ( isset( $defaults ) && !empty( $defaults ) ){
$return = shortcode_atts($defaults,$atts);
}
else{
$return = $atts;
}
return $return;
}
function get_default_atts($shortcode, $filter = false ){
$item_defaults = $meta_defaults = array();
switch($shortcode){
case 'the_app':
$meta_defaults = array(
'_is_debug_on' => false, // sends Javascript messages to console.log() via a maybelog() call. Loads debug version of Sencha library.
'_is_using_manifest' => false, // saves data to the user's device. An app gets 5MB for free without having to ask the user for permission.
'storage_engine' => 'localstorage', // (experimental) - could be localstorage (default) or sqlitestorage
'transition' => 'slide', // slide (default), fade, pop, flip, cube, wipe (buggy) --+ the CSS3 transition to use between pages
'manifest_version' => '', // a version number for the manifest file. Useful for forcing new manifest load.
'maxtabbaritems' => '', // If you want to enable a slide-up menu panel to appear when there are more than N tabs, enter N for maxtabbaritems
'splash_pause' => 2, // If you have a splashscreen, you can force it to display for N seconds by setting splash_pause=N
'ios_install_popup' => false, // True to enable the Install App popup on iOS devices,
'sdk' => '2.3.1', // the Sencha Touch SDK to use - only valid value currently is 2.3.1
'theme' => 'sencha-touch', // valid values are base, bb10, sencha-touch (default). The blank SDK also have wp-app-factory
'menu_style' => 'tabbar', // could be 'tabbar' or 'sheet',
);
break;
case 'launch':
$meta_defaults = array(
'show_all' => true, // Whether to show all slides before launching app, ( false to just launch when ready )
'slide_pause' => 2000, // Millisecond delay between slides
'text_color' => '#000', // The color for the user-defined text messages
'text_top' => '10%', // The top of the user-defined texts
'text_background' => 'none',// The background of the user-defined text box
'message_color' => '#000', // The color of the app-defined messages
'message_top' => '80%', // The top of the app-defined messages
'message_background' => 'none',// The background of the app-defined messages
);
break;
case 'launch_item':
$meta_defaults = array(
'text' => null, // The text to display for this slide
'image' => null, // The image to display for this slide
// If any of these are null, then the defaults for the 'launch' config are used instead
'slide_pause' => null, // Millisecond delay for this slide
'text_color' => null, // The color for the text and the loading message for this slide
'text_top' => null, // The top of the user-defined text
'text_background' => null, // The background of the text box
'message_color' => null, // The color of the app-defined message
'message_top' => null, // The top of the app-defined message
'message_background' => null,// The background of the app-defined message
);
break;
case 'app_item':
$item_defaults = array(
'xtype' => 'htmlpage', // the xtype for the container
'id' => '', // the id for the container.
'icon' => 'star', // action, add, arrow_down, arrow_left, arrow_right, arrow_up, compose, delete, organize, refresh, reply, search, settings, star (default), trash, maps, locate, home
'title' => '', // the title of page. Also the title on the bottom toolbar icon.
'content' => '',
'destroyOnDeactivate' => true, // For LazyPanels, whether or not to destroy the panel when deactivating
);
$meta_defaults = array(
'_is_default' => false, // makes this item the first one that appears.
'template' => '{content}', // the XTemplate to use to display the content
'callback' => null, // a function to call to setup the page. Gives developers finer control
);
break;
case 'app_item_wrapper':
$item_defaults = array(
'xtype' => 'itemwrapper', // the xtype for the container
'id' => '', // the id for the container.
'icon' => 'star', // action, add, arrow_down, arrow_left, arrow_right, arrow_up, compose, delete, organize, refresh, reply, search, settings, star (default), trash, maps, locate, home
'title' => '', // the title of page. Also the title on the bottom toolbar icon.
'pages' => array(), // data for the list, should be array of values with keys 'item' & 'meta'
'destroyOnDeactivate' => true, // For LazyPanels, whether or not to destroy the panel when deactivating
);
$meta_defaults = array(
'_is_default' => false, // makes this item the first one that appears.
'ui' => 'round', // could be round or normal
'list_template' => '{item.title}' // the Sencha tpl for the list item
);
break;
case 'app_posts':
$item_defaults = array(
'xtype' => 'itemlist', // the xtype for the container
'store' => 'postStore', // The store
'id' => '', // the id for the container.
'icon' => 'star', // action, add, arrow_down, arrow_left, arrow_right, arrow_up, compose, delete, organize, refresh, reply, search, settings, star (default), trash, maps, locate, home
'title' => 'Posts', // the title of page. Also the title on the bottom toolbar icon.
'destroyOnDeactivate' => true, // For LazyPanels, whether or not to destroy the panel when deactivating
'infinite' => true // infinite scrolling
);
$meta_defaults = array(
'_is_default' => false, // makes this item the first one that appears.
'store' => 'postStore', // The store
'title' => 'Posts',
'query_vars' => $this->simplify_atts( $this->get( 'query_defaults' ) ),
'grouped' => true, // whether to create group headers
'group_by' => 'first_letter', // first_letter, category, month
'group_order' => 'ASC', // the order for the group headers
'orderby' => 'title', // what to sort the posts on
'order' => 'ASC', // the direction
'indexbar' => true, // whether to create index bar
'numberposts' => -1, // the maximum number of posts to show
'searchable' => false, // Include a search field at the top of the list
'searchableFields' => 'title', // which fields are searchable - accepts a comma separated list
// the Sencha tpl for the list item
'storage_engine' => 'localstorage', // if there is a lot of data, try using 'sqlitestorage'
'list_template' => '<div class="avatar"<tpl if="thumbnail"> style="background-image: url({thumbnail})"</tpl>></div><span class="name">{title}</span>',
// the Sencha tpl for the detail page
'detail_template' => '<tpl if="thumbnail"><img class="thumbnail" src="{thumbnail}"></tpl></div><h3>{title}</h3> {content}'
);
// Add in anything allowed by @get_posts();
$get_post_defaults = WP_Query::fill_query_vars(array());
$meta_defaults = array_merge($get_post_defaults,$meta_defaults,$this->get('query_defaults'));
break;
case 'unacceptable_browser':
$meta_defaults = array(
'not_webkit' => true, // this should ALWAYS be true. Displays Unacceptable Browser message if browser is not webkit
'desktop' => false // display the Unacceptable Browser message if it's a desktop browser
);
break;
}
if ( !$filter ){
$defaults = array_merge( $item_defaults, $meta_defaults );
}
else{
switch( $filter ){
case 'item':
$defaults = $item_defaults;
break;
case 'meta':
$defaults = $meta_defaults;
break;
}
}
if ( !is_array( $defaults ) ){
$defaults = array();
}
return apply_filters('the_app_shortcode_defaults',$defaults,$shortcode);
}
function addHTMLItem($atts){
// HTML pages are easy
$shortcode = 'app_item';
$atts = $this->sanitize_atts( $atts, $shortcode );
$item_atts = shortcode_atts( $this->get_default_atts( $shortcode, 'item' ), $atts );
$meta_atts = shortcode_atts( $this->get_default_atts( $shortcode, 'meta' ), $atts );
static $html_page_counter;
if (!isset($html_page_counter)){
$html_page_counter = 1;
}
$html_store_contents = $this->get('html_store_contents');
if (empty($html_store_contents)){
$html_store_contents = array();
}
if (empty($item_atts['id'])){
$item_atts['id'] = 'htmlpage_'.$html_page_counter;
}
$html_store_contents[] = array(
'id' => $html_page_counter++,
'key' => $item_atts['id'],
'title' => $item_atts['title'],
'content' => $item_atts['content']
);
unset($item_atts['content']);
//unset($item_atts['title']);
$this->set('html_store_contents', $html_store_contents);
$this->addItem($item_atts,$meta_atts);
}
function addWrapperItem($atts){
$shortcode = 'app_item_wrapper';
$atts = $this->sanitize_atts( $atts, $shortcode );
$item_atts = shortcode_atts( $this->get_default_atts( $shortcode, 'item' ), $atts );
$meta_atts = shortcode_atts( $this->get_default_atts( $shortcode, 'meta' ), $atts );
static $wrapper_counter;
if (!isset($wrapper_counter)){
$wrapper_counter = 1;
}
$wrapper_store_contents = $this->get('wrapper_store_contents');
if (empty($wrapper_store_contents)){
$wrapper_store_contents = array();
}
if (empty($item_atts['id'])){
$item_atts['id'] = 'wrapper_'.$wrapper_counter;
}
$wrapper_store_contents[] = array(
'id' => $wrapper_counter++,
'key' => $item_atts['id'],
'title' => $item_atts['title'],
'pages' => $item_atts['pages']
);
unset($item_atts['pages']);
//unset($item_atts['title']);
$this->set('wrapper_store_contents', $wrapper_store_contents);
$this->addItem($item_atts,$meta_atts);
}
function addPostListItem($atts){
$shortcode = 'app_posts';
$atts = $this->sanitize_atts( $atts, $shortcode );
$meta_defaults = $this->get_default_atts( $shortcode, 'meta' );
$query_defaults = $this->get('query_defaults');
$query_atts = $this->simplify_atts(shortcode_atts($query_defaults,$atts));
$atts[ 'store' ] = $query_atts[ 'post_type' ] . 'Store';
$atts[ 'query_vars' ] = $query_atts;
$item_atts = shortcode_atts( $this->get_default_atts( $shortcode, 'item' ), $atts );
$meta_atts = shortcode_atts( $meta_defaults, $atts );
$index = $this->registerPostQuery($meta_atts);
$item_atts['queryInstance'] = $index;
if ( $this->get( 'sdk' ) >= '2.3.1' && !$meta_atts['grouped'] ){
// There's a bug in Sencha Touch 2.3.1 where if a list is not grouped, but is infinite, it throws an error
$item_atts['infinite'] = false;
}
if ( isset( $meta_atts['searchable'] ) && $meta_atts['searchable'] ){
$this->enqueue('controller','Search');
}
$this->addItem($item_atts,$meta_atts);
}
function addItem($atts,$meta = array()){
$new_item = array();
if (isset($atts['icon'])){
$atts['iconCls'] = $atts['icon'];
}
if (isset($atts['id']) and empty($atts['id'])){
unset($atts['id']);
}
$new_item = array('item' => $atts);
if (count($meta)){
$new_item['meta'] = $meta;
}
if ($new_item['meta']['_is_default']){
$items = $this->get('items');
array_unshift($items,$new_item);
$this->set('items',$items);
}
elseif ($this->is('capturing')){
$captured_items = $this->get('captured_items');
$captured_items[] = $new_item; //['item'];
$this->set('captured_items',$captured_items);
}
else{
$items = $this->get('items');
$items[] = $new_item;
$this->set('items',$items);
}
if ( $this->is_registered( 'view', $atts['xtype'] ) ){
$this->enqueue('view',$atts['xtype']);
}
if ($atts['xtype'] == 'itemlist'){
$this->enqueue('view','ItemDetail');
}
}
private function setupModels(){
$models = array();
if ($this->is('using_manifest')){
$models['StoreStatus'] = array('fields' => array('id','store','timestamp'));
}
if ($this->get('wrapper_store_contents')){
$models['WrapperPage'] = array('fields' => array('id','title','pages','key'));
}
if ($this->get('html_store_contents')){
$models['HtmlPages'] = array('fields' => array('id','title','content','key'));
}
if ( 'sheet' == $this->get( 'menu_style' ) ){
$models['SheetMenuItems'] = array( 'fields' => array( 'id', 'text', 'items', 'iconCls' ) );
}
$this->set('models',apply_filters('TheAppFactory_models',$models,array(&$this)));
do_action_ref_array('TheAppFactory_setupModels',array(&$this));
}
private function setupHelpers(){
$helpers = array();
global $month;
$queryFilter = "
function(panel){
return new Ext.util.Filter({
filterFn: function(item){
return item.get('query_num').match(new RegExp('_'+panel.queryInstance+'_')) && (panel.meta.group_by == 'category' || item.get('spoof_id') == undefined);
}
});
}
";
$helpers['WP'] = array(
'months' => array_values($month),
'_' => $this->do_not_escape('function(s){ return s; }'), // Sencha 2 adds a '_' in front of the property, so this will become WP.__()
'queryFilter' => $this->do_not_escape(preg_replace('/[\n\r\t]/','',$queryFilter)),
'ajaxUrl' => admin_url('admin-ajax.php'),
'url' => get_permalink(),
'ID' => get_the_ID(),
'appId' => md5(get_the_ID()), // This is the app id in the app.json file
'localStorageKey' => $this->get( 'app_id' ),
'appName' => get_the_title()
);
$this->set('helpers',apply_filters('TheAppFactory_helpers',$helpers,array(&$this)));
do_action_ref_array('TheAppFactory_setupHelpers',array(&$this));
}
private function setupProfiles(){
$profiles = array();
$this->set('profiles',apply_filters('TheAppFactory_profiles',$profiles,array(&$this)));
do_action_ref_array('TheAppFactory_setupProfiles',array(&$this));
}
private function registerPostQuery($meta_atts){
$registered_post_queries = $this->get('registered_post_queries');
if (isset($meta_atts['query_vars']['post_type'])){
$post_type = $meta_atts['query_vars']['post_type'];
}
elseif(isset($meta_atts['query_vars']['xtype'])){
$post_type = $meta_atts['query_vars']['xtype'];
}
if (!isset($post_type)){
die(__('Attempting to register a list without a post type. Please set either $atts[query_vars][post_type] or $atts[query_vars][xtype] when calling TheAppFactory::addPostListItem','app-factory'));
}
if (!array_key_exists($post_type,$registered_post_queries)){
$registered_post_queries[$post_type] = array();
}
$registered_post_queries[$post_type][] = $meta_atts;
$this->set('registered_post_queries',$registered_post_queries);
return (count($registered_post_queries[$post_type]) - 1); // the query instance index
}
function addRegisteredModels($models,$_this){
if (is_array($this->get('registered_post_queries'))){
foreach ($this->get('registered_post_queries') as $post_type => $registered_meta){
$callback_exists = false;
foreach($registered_meta as $queryInstance => $registered_query){
if (isset($registered_query['query_vars']['model_callback'])){
$post_type = $registered_query['query_vars']['post_type'];
$models[$post_type] = call_user_func($registered_query['query_vars']['model_callback'],$post_type);
if (!in_array('query_num',$models[$post_type]['fields'])){
$models[$post_type]['fields'][] = 'query_num';
}
$callback_exists = true;
}
}
if ($callback_exists){
continue;
}
$models[$post_type] = array('fields' => array());
// common parms are parms that are common to all post types
// We'll just get them from the app post
foreach ($this->get('post') as $field => $value){
if ($field != 'post_password'){
$field = $this->sanitize_key($field);
$models[$post_type]['fields'][] = $field;
}
}
// Now, we need to get all custom fields that exist for posts of this type
// Turns out to be tricker than I thought. I don't know if WP has something
// native for this...
$meta_keys = $this->lookupCustomFields($post_type);
foreach ($meta_keys as $key){
$models[$post_type]['fields'][] = $key;
}
// Finally, let's add one for the thumbnail
$models[$post_type]['fields'][] = 'thumbnail';
// If there are more than one queries registered for this post_type,
// we'll add another field which will indicate which registered_query
// we're dealing with
$models[$post_type]['fields'][] = 'query_num';
// Add for category
$models[$post_type]['fields'][] = 'category';
$models[$post_type]['fields'][] = 'spoof_id'; // allows a single post to appear under more than one category
}
}
return $models;
}
public function lookupCustomFields($post_type){
global $wpdb;
static $query;
if (!isset($query)){
$query = "SELECT DISTINCT `meta_key` FROM $wpdb->postmeta where `post_id` in (SELECT ID FROM $wpdb->posts WHERE `post_type` = %s)";
}
$meta_keys = $wpdb->get_col($wpdb->prepare($query, $post_type));
foreach ($meta_keys as $k => $key){
if (substr($key,0,1) == '_'){
unset($key->$k);
}
}
return $meta_keys;
}
private function setupStores(){
$stores = array();
if ($this->get('wrapper_store_contents')){
$stores['WrapperPageStore'] = array(
'fields' => array('id','title','content')
);
$stores['WrapperPageStore'] = array(
'model' => 'WrapperPage',
'autoLoad' => true,
'proxy' => array(
'type' => 'scripttag',
'url' => $this->get('mothership').'data/wrapperpages',
'reader' => array('type' => 'json', 'rootProperty' => 'wrapperpages')
)
);
if ($this->is('using_manifest')){
$stores['WrapperPageStore']['useLocalStorage'] = true;
}
}
if ($this->get('html_store_contents')){
$stores['HtmlPagesStore'] = array(
'model' => 'HtmlPages',
'autoLoad' => true,
'proxy' => array(
'type' => 'scripttag',
'url' => $this->get('mothership').'data/htmlpages',
'reader' => array('type' => 'json', 'rootProperty' => 'htmlpages')
)
);
if ($this->is('using_manifest')){
$stores['HtmlPagesStore']['useLocalStorage'] = true;
}
}
$this->set('stores',apply_filters('TheAppFactory_stores',$stores,array(&$this)));
do_action_ref_array('TheAppFactory_setupStores',array(&$this));
}
public function setupStoreStatusStore($the_app){
if ($this->is('using_manifest')){
$stores = $this->get('stores');
$stores['StoreStatusStore'] = array();
$stores['StoreStatusStore']['model'] = 'StoreStatus';
$stores['StoreStatusStore']['useLocalStorage'] = true;
$stores['StoreStatusStore']['autoLoad'] = true; // Note camelCase...
$stores['StoreStatusStore']['proxy'] = array(
'type' => 'scripttag',
'url' => $this->get('mothership').'data/storemeta',
'reader' => array('type' => 'json', 'rootProperty' => 'stores')
);
$stores = $this->set('stores',$stores);
}
}
public function massageStoreConfigs( & $the_app ){
$stores = $this->get( 'stores' );
foreach( $stores as $key => $store ){
if (!isset($store['storeId'])){
$store['storeId'] = $key;
}
/*
// because of other functionality, I'm going to hold off on this and only do it in the actual store factory
if( isset( $store['model'] ) and strpos( $store['model'], 'the_app.model' ) === false){
$store['model'] = 'the_app.model.' . $store['model'];
}
*/
// Setup offline versino of the store
if ( $store[ 'useLocalStorage' ] ){
$store[ 'serverProxy' ] = $store[ 'proxy' ];
if ( !isset( $store[ 'storage_engine'] ) ){
$store[ 'storage_engine'] = $the_app->get( 'storage_engine' );
}
switch( $store[ 'storage_engine' ] ){
case 'localstorage':