1: <?php
2: 3: 4:
5: class Sidecar_Plugin_Base extends Sidecar_Singleton_Base {
6: 7: 8:
9: var $plugin_class;
10:
11: 12: 13:
14: var $plugin_class_base;
15:
16: 17: 18:
19: var $plugin_name;
20:
21: 22: 23: 24: 25:
26: var $plugin_slug;
27:
28: 29: 30: 31: 32:
33: var $plugin_id;
34:
35: 36: 37:
38: var $plugin_version;
39:
40: 41: 42:
43: var $plugin_title;
44:
45: 46: 47:
48: var $plugin_label;
49:
50: 51: 52:
53: var $css_base;
54:
55: 56: 57:
58: var $plugin_file;
59:
60: 61: 62:
63: var $plugin_path;
64:
65: 66: 67:
68: var $min_php = '5.2.4';
69:
70: 71: 72:
73: var $min_wp = '3.2';
74:
75:
76:
77:
78:
79:
80:
81:
82:
83: 84: 85:
86: protected $_urls = array();
87:
88: 89: 90:
91: protected $_images = array();
92:
93: 94: 95:
96: protected $_shortcodes = false;
97:
98: 99: 100:
101: protected $_forms = false;
102:
103: 104: 105:
106: protected $_admin_pages = array();
107:
108: 109: 110:
111: protected $_meta_links = array();
112:
113: 114: 115:
116: protected $_api = false;
117:
118: 119: 120:
121: protected $_current_admin_page;
122:
123: 124: 125:
126: protected $_current_form;
127:
128: 129: 130:
131: protected $_initialized = false;
132:
133: 134: 135:
136: protected $_plugin_settings;
137:
138: 139: 140:
141: protected $_default_settings_values;
142:
143: 144: 145:
146: var $option_name;
147:
148: 149: 150:
151: var $needs_ajax = false;
152:
153: 154: 155:
156: var $needs_settings = true;
157:
158: 159: 160:
161: protected $_admin_initialized = false;
162:
163:
164:
165:
166: 167: 168: 169:
170: function set_api_loader( $class_name, $filepath ) {
171: $this->_api = array(
172: 'class_name' => $class_name,
173: 'filepath' => $filepath,
174: );
175: }
176:
177: 178: 179:
180: function has_api() {
181: return false !== $this->get_api();
182: }
183:
184: 185: 186: 187:
188: function get_api() {
189: if ( ! $this->_api instanceof RESTian_Client ) {
190: 191: 192:
193: $filepath= '/' == $this->_api['filepath'][0] ? $this->_api['filepath'] : "{$this->plugin_path}/{$this->_api['filepath']}";
194:
195: if ( is_file( $filepath) ) {
196: require_once( $filepath );
197: if ( class_exists( $this->_api['class_name'] ) ) {
198: $class_name = $this->_api['class_name'];
199:
200: $this->_api = new $class_name( $this );
201: }
202: 203: 204:
205: $this->_api->initialize_client();
206: }
207: }
208: return $this->_api;
209: }
210:
211: 212: 213:
214: function set_api( $api ) {
215: if ( empty( $this->_forms ) ) {
216: 217: 218:
219: $error_message = __( '$plugin->set_api($api) cannot be called before forms have been added. Please call $plugin->add_form() in $plugin->initialize_plugin() at least once prior to calling set_api().', 'sidecar' );
220: trigger_error( $error_message );
221: exit;
222: }
223: $api->caller = $this;
224: $api->initialize_client();
225: $this->_api = $api;
226: if ( $this->_admin_initialized ) {
227: 228: 229: 230: 231:
232: $this->_api->set_grant( $this->get_grant() );
233: }
234: }
235:
236: function is_saving_widget() {
237: global $pagenow;
238: return isset( $_POST['action'] ) && isset( $_POST['id_base'] )
239: && 'admin-ajax.php' == $pagenow && 'save-widget' == $_POST['action'];
240: }
241: 242: 243:
244: function on_load( $args = array() ) {
245:
246: 247: 248: 249: 250:
251: if ( ! $this->needs_ajax && $this->is_saving_widget() ) {
252: $this->needs_ajax = true;
253: }
254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264:
265: if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ( ! $this->needs_ajax || ( isset( $args['needs_ajax'] ) && ! $args['needs_ajax'] ) ) )
266: return;
267:
268: $this->plugin_class = get_class( $this );
269:
270: 271: 272:
273: foreach( $args as $property => $value )
274: if ( property_exists( $this, $property ) )
275: $this->$property = $value;
276:
277: add_action( 'init', array( $this, '_init' ) );
278: add_action( 'wp_loaded', array( $this, '_wp_loaded' ) );
279: add_action( 'wp_print_styles', array( $this, '_wp_print_styles' ) );
280: add_action( 'save_post', array( $this, '_save_post' ) );
281:
282: $this->plugin_class_base = preg_replace( '#^(.*?)_Plugin$#', '$1', $this->plugin_class );
283:
284: if ( ! $this->plugin_name )
285: $this->plugin_name = strtolower( $this->plugin_class_base );
286:
287: if ( ! $this->option_name )
288: $this->option_name = "{$this->plugin_name}_settings";
289:
290:
291:
292:
293: if ( $this->is_plugin_page_action() ) {
294: global $plugin;
295: if ( ! isset( $plugin ) ) {
296: 297: 298:
299: $this->plugin_id = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_STRING );
300: $this->plugin_file = WP_PLUGIN_DIR . '/' . $this->plugin_id;
301: } else if ( file_exists( $plugin ) ) {
302: 303: 304:
305: $this->plugin_file = $plugin;
306: $this->plugin_id = basename( dirname( $plugin ) ) . '/' . basename( $plugin );
307: } else {
308: 309: 310:
311: $this->plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
312: $this->plugin_id = $plugin;
313: }
314: add_action( 'plugins_loaded', array( $this, '_plugins_loaded' ) );
315: add_action( "activate_{$this->plugin_id}", array( $this, '_activate_plugin' ), 0 );
316: register_activation_hook( $this->plugin_id, array( $this, '_activate' ) );
317: } else if ( ! WP_Library_Manager::$loading_plugin_loaders && $this->is_verified_plugin_deletion() ) {
318: if ( preg_match( '#^uninstall_(.*?)$#', current_filter(), $match ) ) {
319: $this->plugin_file = WP_PLUGIN_DIR . "/{$match[1]}";
320: } else {
321: 322: 323: 324:
325: $backtrace = debug_backtrace();
326: foreach( $backtrace as $index => $call ) {
327: if ( preg_match( '#/wp-admin/includes/plugin.php$#', $call['file'] ) ) {
328: $this->plugin_file = $backtrace[$index-1]['file'];
329: break;
330: }
331: }
332: }
333: $this->plugin_id = basename( dirname( $this->plugin_file ) ) . '/' . basename( $this->plugin_file );
334: } else if ( false !== WP_Library_Manager::$uninstalling_plugin && preg_match( '#' . preg_quote( WP_Library_Manager::$uninstalling_plugin ) . '$#', $GLOBALS['plugin'] ) ) {
335: 336: 337: 338:
339: global $plugin;
340: $this->plugin_file = $plugin;
341: $this->plugin_id = WP_Library_Manager::$uninstalling_plugin;
342: } else {
343: 344: 345: 346: 347:
348: global $mu_plugin, $network_plugin, $plugin;
349: if ( isset( $mu_plugin ) ) {
350: $this->plugin_file = $mu_plugin;
351: } else if ( isset( $network_plugin ) ) {
352: $this->plugin_file = $network_plugin;
353: } else if ( isset( $plugin ) ) {
354: $this->plugin_file = $plugin;
355: } else {
356: trigger_error( sprintf( __( 'Plugin %s only works when loaded by WordPress.' ), $this->plugin_name ) );
357: exit;
358: }
359: $this->plugin_id = basename( dirname( $this->plugin_file ) ) . '/' . basename( $this->plugin_file );
360: require_once(ABSPATH . 'wp-admin/includes/plugin.php');
361: if ( ! is_plugin_active( $this->plugin_id ) ) {
362: trigger_error( sprintf( __( 'Plugin %s is not an active plugin or is not installed in a subdirectory of %s.' ),
363: $this->plugin_name,
364: WP_PLUGIN_DIR
365: ));
366: exit;
367: }
368: register_deactivation_hook( $this->plugin_id, array( $this, 'deactivate' ) );
369: }
370:
371: register_uninstall_hook( $this->plugin_id, array( $this->plugin_class, 'uninstall' ) );
372:
373: $this->_plugin_settings = new Sidecar_Plugin_Settings( $this, $this->option_name );
374:
375: 376: 377:
378: $this->initialize_plugin();
379:
380: }
381:
382: 383: 384: 385:
386: function _save_post( $post_id ) {
387: $this->initialize_shortcodes();
388: $shortcodes = $this->get_shortcodes();
389: if ( is_array( $shortcodes ) ) {
390: 391: 392:
393: foreach( $shortcodes as $shortcode )
394: $shortcode->delete_has_shortcode( $post_id );
395: 396: 397:
398: wp_remote_request( get_permalink( $post_id ), array( 'blocking' => false ) );
399: }
400: }
401:
402: 403: 404: 405: 406:
407: function is_confirm_plugin_deletion() {
408: return $this->is_plugin_deletion()
409: && ! isset( $_POST['verify-delete'] );
410: }
411:
412: 413: 414: 415: 416:
417: function is_verified_plugin_deletion() {
418: return $this->is_plugin_deletion()
419: && isset( $_POST['verify-delete'] ) && '1' == $_POST['verify-delete'];
420: }
421:
422: 423: 424: 425: 426:
427: function is_plugin_deletion() {
428: return $this->is_plugin_page()
429: && isset( $_GET['action'] ) && 'delete-selected' == $_GET['action']
430: && isset( $_REQUEST['checked'] ) && count( $_REQUEST['checked'] );
431: }
432:
433: 434: 435:
436: static function uninstall() {
437:
438: 439: 440:
441: $plugin = self::this();
442:
443: 444: 445:
446: $plugin->initialize();
447:
448: 449: 450:
451: $plugin->delete_settings();
452:
453: 454: 455:
456: $plugin->uninstall_plugin();
457:
458:
459:
460:
461:
462:
463:
464: }
465:
466: 467: 468: 469: 470:
471: function is_plugin_page() {
472: global $pagenow;
473: return 'plugins.php' == $pagenow;
474: }
475:
476: 477: 478: 479: 480:
481: function is_plugin_page_action() {
482: return $this->is_plugin_page()
483: && isset( $_GET['action'] )
484: && isset( $_GET['plugin'] );
485: }
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497: 498: 499: 500:
501: function _activate_plugin() {
502: $this->initialize();
503: }
504:
505: 506: 507: 508:
509: function _plugins_loaded() {
510: $this->initialize();
511: }
512:
513: 514: 515:
516: function get_forms() {
517: foreach( $this->_forms as $form_name => $form )
518: if ( is_array( $form ) )
519: $this->_forms[$form_name] = $this->promote_form( $form );
520: return $this->_forms;
521: }
522:
523: 524: 525:
526: function has_forms() {
527: return is_array( $this->_forms ) && count( $this->_forms );
528: }
529:
530: 531: 532:
533: function _wp_print_styles() {
534: $localfile = 'css/style.css';
535: $args = apply_filters( "sidecar_print_{$this->plugin_name}_styles", array(
536: 'name' => "{$this->plugin_name}_style",
537: 'path' => "{$this->plugin_path}/{$localfile}",
538: 'url' => plugins_url( $localfile, $this->plugin_file ),
539: ));
540: if ( file_exists( $args['path'] ) )
541: wp_enqueue_style( $args['name'], $args['url'] );
542: }
543:
544: 545: 546:
547: function has_admin_pages() {
548: return 0 < count( $this->_admin_pages );
549: }
550: 551: 552: 553: 554:
555: function has_form( $form_name ) {
556: return isset( $this->_forms[$form_name] );
557: }
558:
559: function initialize() {
560: if ( $this->_initialized )
561: return;
562:
563: 564: 565:
566: $this->_initialized = true;
567:
568: if ( ! $this->plugin_title )
569: 570: 571:
572: $this->plugin_title = ucwords( str_replace( '_', ' ', $this->plugin_name ) );
573:
574: if ( ! $this->plugin_label )
575: 576: 577:
578: $this->plugin_label = $this->plugin_title;
579:
580: if ( ! $this->plugin_file )
581: Sidecar::show_error( '%s->plugin_file must be set in an %s->initialize_admin() method', get_class( $this), get_class( $this) );
582:
583:
584:
585: if ( ! $this->plugin_path )
586: $this->plugin_path = dirname( $this->plugin_file );
587:
588: if ( ! $this->plugin_slug )
589: $this->plugin_slug = str_replace( '_', '-', $this->plugin_name );
590:
591: if ( ! $this->css_base )
592: $this->css_base = $this->plugin_slug;
593:
594: $this->_plugin_settings->load_settings();
595:
596: }
597:
598: 599: 600:
601: function get_default_settings_values() {
602: if ( ! isset( $this->_default_settings_values ) ) {
603: $default_settings_values = array();
604: foreach( $this->get_forms() as $form ) {
605: 606: 607:
608: $default_settings_values[$form->form_name] = $form->get_default_settings_values();
609: }
610: $this->_default_settings_values = $default_settings_values;
611: }
612: return $this->_default_settings_values;
613: }
614:
615: 616: 617:
618: function update_settings_values( $settings_values ) {
619: $this->get_settings()->set_values( $settings_values );
620: }
621:
622: 623: 624: 625: 626:
627: function get_form_settings_value( $form, $setting_name ) {
628: if ( ! $form instanceof Sidecar_Form )
629: $form = $this->get_form( $form );
630: return $this->get_form_settings( $form )->get_setting( $setting_name );
631:
632: }
633:
634: 635: 636: 637: 638: 639:
640: function update_form_settings_value( $form, $setting_name, $value ) {
641: if ( ! $form instanceof Sidecar_Form )
642: $form = $this->get_form( $form );
643: return $form->update_settings_value( $setting_name, $value );
644: }
645:
646: 647: 648:
649: function has_required_settings() {
650: $has_required_settings = true;
651: if ( ! $this->_initialized )
652: $this->initialize();
653: if ( $this->has_forms() ) {
654:
655: foreach( $this->get_forms() as $form_name => $form ) {
656: $form_settings = $this->get_form_settings( $form->form_name );
657: $form_settings->set_required_fields( $form->get_required_field_names() );
658: if ( ! $form_settings->has_required_fields() ) {
659: $has_required_settings = false;
660: break;
661: }
662: }
663: }
664: if ( method_exists( $this, 'filter_has_required_settings' ) ) {
665: $has_required_settings = $this->filter_has_required_settings( $has_required_settings, $this->get_settings() );
666: }
667: return $has_required_settings;
668: }
669:
670:
671: 672: 673:
674: function set_settings( $settings ) {
675: $this->_plugin_settings = $settings;
676: }
677:
678: 679: 680:
681: function get_settings() {
682: if ( ! $this->_initialized )
683: $this->initialize();
684: return $this->_plugin_settings;
685: }
686:
687: 688: 689: 690:
691: function get_form_field_values( $form ) {
692: return $this->get_form_settings( $form );
693: }
694:
695: 696: 697: 698: 699:
700: function get_form_field_value( $form, $field_name ) {
701: return $this->get_form_settings( $form )->get_values();
702: }
703:
704: 705: 706: 707: 708:
709: function set_form_field_values( $form, $field_values ) {
710: $this->set_form_settings( $form, $field_values );
711: }
712:
713: 714: 715: 716:
717: function get_form_settings( $form ) {
718: if ( ! $form instanceof Sidecar_Form )
719: $form = $this->get_form( $form );
720: return $this->get_settings()->get_setting( $form->form_name );
721: }
722:
723: 724: 725: 726: 727:
728: function set_form_settings( $form, $setting_value ) {
729: if ( ! $form instanceof Sidecar_Form )
730: $form = $this->get_form( $form );
731: $this->get_settings()->set_setting( $form->form_name, $setting_value );
732: }
733:
734: 735: 736: 737: 738:
739: function delete_settings() {
740: $this->get_settings()->delete_settings();
741: }
742:
743: 744: 745:
746: function _wp_loaded() {
747: if ( is_admin() ) {
748: add_action( 'admin_notices', array( $this, '_admin_notices' ) );
749: if ( $this->is_plugin_page() ) {
750: add_filter( 'plugin_action_links', array( $this, '_plugin_action_links' ), 10, 2 );
751: add_filter( 'plugin_row_meta', array( $this, '_plugin_meta_links' ), 10, 2 );
752: }
753: } else {
754: $shortcodes = method_exists( $this->plugin_class, 'initialize_shortcodes' );
755: $template = method_exists( $this, 'initialize_template' );
756: if ( $shortcodes || $template ) {
757:
758: $this->initialize();
759: if ( $shortcodes )
760: $this->initialize_shortcodes();
761: if ( $template )
762: $this->initialize_template();
763: add_filter( 'the_content', array( $this, '_the_content' ), -1000 );
764: }
765: }
766: }
767: 768:
769: function _admin_notices() {
770: if ( $this->needs_settings && ! $this->has_required_settings() && $this->is_plugin_page() && ! $this->is_confirm_plugin_deletion() ) {
771: $icon_html = $this->has_url( 'logo_icon' ) ? "<span class=\"sidecar-logo-icon\"></span><img src=\"{$this->logo_icon_url}\" /></span>" : '';
772: $message = sprintf( __( 'The <em>%s</em> plugin is now activated. Please configure it\'s <a href="%s"><strong>settings</strong></a>.', 'sidecar' ),
773: $this->plugin_title,
774: $this->get_settings_url()
775: );
776: $html = <<<HTML
777: <div id="message" class="error settings-error">
778: <p>{$icon_html}{$message}</p>
779: </div>
780: HTML;
781: echo $html;
782: }
783: }
784:
785: 786: 787: 788: 789: 790:
791: function _plugin_action_links( $links, $file ){
792: if ( $file == $this->plugin_id ) {
793: $url = $this->get_settings_url();
794: $link_text = __( 'Settings', 'sidecar' );
795: $links[] = "<a href=\"{$url}\">{$link_text}</a>";
796: }
797: return $links;
798: }
799:
800: 801: 802:
803: function get_settings_url() {
804: $settings_url = false;
805: if ( $settings_page = $this->get_admin_page( 'settings' ) ) {
806: $settings_page->initialize();
807: $settings_url = $this->get_admin_page( 'settings' )->get_page_url( null );
808: }
809: if ( method_exists( $this, 'filter_settings_url' ) )
810: $settings_url = $this->filter_settings_url( $settings_url );
811: return $settings_url;
812: }
813: 814: 815: 816: 817: 818:
819: function _plugin_meta_links( $links, $file ){
820: if ( $file == $this->plugin_id ) {
821: foreach( $this->_meta_links as $link_text => $link ) {
822: $title = isset( $link['title'] ) ? " title=\"{$link['title']}\"" : '';
823: $links[] = "<a target=\"_blank\" href=\"{$link['url']}\"{$title}>{$link_text}</a>";
824: }
825: }
826: return $links;
827: }
828:
829: 830: 831:
832: function _the_content( $content ) {
833: $shortcodes = $this->get_shortcodes();
834: if ( is_array( $shortcodes ) )
835: 836: 837:
838: foreach( $shortcodes as $shortcode_name => $shortcode ) {
839: if ( method_exists( $this, 'initialize_shortcode' ) ) {
840: $this->initialize_shortcode( $shortcode );
841: }
842: add_shortcode( $shortcode_name, array( $shortcode, 'do_shortcode' ) );
843: 844: 845:
846: $shortcode->add_the_content_filter();
847: }
848: return $content;
849: }
850:
851: function _init() {
852:
853:
854: 855: 856:
857: load_plugin_textdomain( $this->plugin_slug, false, '/' . basename( dirname( $this->plugin_file ) ) . '/languages' );
858:
859: if ( is_admin() ) {
860:
861: 862: 863:
864: if (true) {
865: 866: 867:
868: $this->initialize();
869:
870: 871: 872:
873: $this->_initialize_admin();
874:
875:
876:
877:
878: }
879: }
880: }
881: 882: 883: 884: 885: 886:
887: function transform_form_fields_to( $to, $form, $fields ) {
888: $to = rtrim( $to, 's' );
889: $field_objects = $form->get_fields();
890: foreach( $fields as $field_name => $value ) {
891: if ( isset( $field_objects[$field_name] ) ) {
892: if ( $field_name != ( $new_name = $field_objects[$field_name]->$to ) ) {
893: if ( $new_name )
894: $fields[$new_name] = $value;
895: unset( $fields[$field_name] );
896: }
897: }
898: }
899: return $fields;
900: }
901:
902: 903: 904: 905: 906: 907:
908: function transform_shortcode_attributes_to( $to, $shortcode, $attributes ) {
909: $to = rtrim( $to, 's' );
910: $attribute_objects = $shortcode->get_attributes();
911: foreach( $attributes as $attribute_name => $attribute ) {
912: if ( isset( $attribute_objects[$attribute_name] ) ) {
913: if ( $attribute_name != ( $new_name = $attribute_objects[$attribute_name]->$to ) ) {
914: $attributes[$new_name] = $attribute;
915: unset( $attributes[$attribute_name] );
916: }
917: }
918: }
919: return $attributes;
920: }
921:
922: 923: 924:
925: function initialize_form( $form ) {
926:
927: }
928:
929: 930: 931:
932: function initialize_shortcodes() {
933:
934: }
935:
936: 937: 938:
939: function initialize_postback() {
940:
941: }
942:
943: 944: 945:
946: function initialize_template() {
947:
948: }
949:
950: 951: 952:
953: function uninstall_plugin() {
954:
955: }
956:
957: 958: 959:
960: function initialize_plugin() {
961: throw new Exception( 'Class ' . get_class($this) . ' [subclass of ' . __CLASS__ . '] must define an initialize_plugin() method.' );
962: }
963:
964: 965: 966:
967: function initialize_admin() {
968: throw new Exception( 'Class ' . get_class($this) . ' [subclass of ' . __CLASS__ . '] must define an initialize_admin() method.' );
969: }
970:
971: 972: 973: 974:
975: function initialize_admin_page( $admin_page ) {
976: throw new Exception( 'Class ' . get_class($this) . ' [subclass of ' . __CLASS__ . '] must define an initialize_admin_page() method.' );
977: }
978:
979: 980: 981: 982:
983: function initialize_shortcode( $shortcode ) {
984: throw new Exception( 'Class ' . get_class($this) . ' [subclass of ' . __CLASS__ . '] must define an initialize_shortcode() method.' );
985: }
986:
987: 988: 989: 990: 991: 992: 993: 994:
995: function do_shortcode( $shortcode, $attributes, $content = null ) {
996: if (1)
997: throw new Exception( 'Class ' . get_class($this) . ' [subclass of ' . __CLASS__ . '] must define an do_shortcode() method.' );
998: return '';
999: }
1000:
1001: 1002: 1003: 1004: 1005:
1006: function add_default_button( $args = array() ) {
1007: 1008: 1009:
1010: $form = isset( $args['form'] ) ? $args['form'] : end( $this->_forms );
1011: return $form->add_button( 'save', __( 'Save Settings', 'sidecar' ), $args );
1012: }
1013:
1014: 1015: 1016: 1017: 1018: 1019: 1020:
1021: function add_button( $button_name, $button_text, $args = array() ) {
1022: 1023: 1024:
1025: $form = isset( $args['form'] ) ? $args['form'] : end( $this->_forms );
1026: return $form->add_button( $button_name, $button_text, $args );
1027: }
1028:
1029: 1030: 1031: 1032: 1033: 1034:
1035: function add_admin_page( $page_name, $args = array() ) {
1036: 1037: 1038:
1039: $args['plugin'] = $this;
1040: 1041: 1042:
1043: $this->_admin_pages[$page_name] = new Sidecar_Admin_Page( $page_name, $args );
1044: }
1045:
1046: 1047: 1048: 1049: 1050:
1051: function add_meta_link( $link_text, $url, $args = array() ) {
1052: $args['link_text'] = $link_text;
1053: $args['url'] = isset( $this->_urls[$url] ) ? $this->_urls[$url]['url_template'] : $url;
1054: $this->_meta_links[$link_text] = $args;
1055: }
1056:
1057: 1058: 1059: 1060: 1061:
1062: function get_admin_page( $page_name ) {
1063: $page_slug = preg_replace( "#^{$this->plugin_slug}-(.*)$#", '$1', $page_name );
1064: return isset( $this->_admin_pages[$page_slug] ) ? $this->_admin_pages[$page_slug] : false;
1065: }
1066:
1067: 1068: 1069:
1070: function add_default_shortcode() {
1071: $this->add_shortcode( $this->plugin_slug );
1072: }
1073:
1074: 1075: 1076: 1077:
1078: function add_shortcode( $shortcode_name, $args = array() ) {
1079: $args['plugin'] = $this;
1080: $this->_shortcodes[ $shortcode_name ] = new Sidecar_Shortcode( $shortcode_name, $args );
1081: }
1082:
1083: 1084: 1085:
1086: function get_shortcodes() {
1087: return $this->_shortcodes;
1088: }
1089:
1090: 1091: 1092: 1093: 1094:
1095: function get_shortcode( $shortcode_name = false ) {
1096: $shortcode = false;
1097:
1098: if ( ! $shortcode_name )
1099: $shortcode_name = $this->plugin_slug;
1100:
1101: if ( ! isset( $this->_shortcodes[$shortcode_name] ) ) {
1102: trigger_error( sprintf( __( 'Need to call %s->initialize_shortcodes() before using %s->get_shortcode().' ), $this->plugin_class, $this->plugin_class ) );
1103: } else {
1104: 1105: 1106:
1107: $shortcode = $this->_shortcodes[$shortcode_name];
1108: if ( ! $shortcode->initialized ) {
1109: $this->initialize_shortcode($shortcode);
1110: $shortcode->initialized = true;
1111: }
1112: }
1113: return $shortcode;
1114: }
1115:
1116: 1117: 1118: 1119: 1120:
1121: function get_shortcode_attributes( $shortcode_name = false ) {
1122: $shortcode = $this->get_shortcode($shortcode_name);
1123: return $shortcode ? $shortcode->get_attributes() : false;
1124: }
1125:
1126:
1127:
1128:
1129:
1130:
1131:
1132:
1133:
1134:
1135:
1136:
1137:
1138:
1139:
1140:
1141:
1142:
1143:
1144:
1145:
1146:
1147:
1148:
1149:
1150:
1151:
1152:
1153:
1154:
1155:
1156:
1157:
1158:
1159: 1160: 1161: 1162: 1163:
1164: function register_url( $url_name, $url_template, $args = array() ) {
1165: $args['url_name'] = $url_name;
1166: $args['url_template'] = $url_template;
1167: if ( ! isset( $args['url_vars'] ) )
1168: $args['url_vars'] = false;
1169: $this->_urls[$url_name] = $args;
1170: }
1171:
1172: 1173: 1174: 1175: 1176:
1177: function has_url( $url_name ) {
1178: return isset( $this->_urls[$url_name] );
1179: }
1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193:
1194: function get_url( $url_name, $values = array() ) {
1195: if ( ! is_array( $values ) ) {
1196: 1197: 1198:
1199: $values = func_get_args();
1200: array_shift( $values );
1201: }
1202: $url = $this->_urls[$url_name]['url_template'];
1203: if ( is_array( $values ) ) {
1204: $vars = $this->_urls[$url_name]['url_vars'];
1205: if ( ! $vars ) {
1206: preg_match_all( '#([^{]+)\{([^}]+)\}#', $url, $matches );
1207: $vars = $matches[2];
1208: }
1209: if ( is_numeric( key($values) ) ) {
1210: 1211: 1212:
1213: foreach( $values as $name => $value ) {
1214: if ( isset( $vars[0] ) )
1215: $url = str_replace( "{{$vars[0]}}", $value, $url );
1216: }
1217: } else {
1218: 1219: 1220:
1221: foreach( $vars as $name ) {
1222: if ( isset( $values[$name] ) )
1223: $url = str_replace( "{{$name}}", $values[$name], $url );
1224: }
1225: }
1226: }
1227: return $url;
1228: }
1229:
1230: 1231: 1232: 1233: 1234:
1235: function get_link_text( $url_name ) {
1236: return isset( $this->_urls[$url_name]['link_text'] ) ? $this->_urls[$url_name]['link_text'] : false;
1237: }
1238:
1239: 1240: 1241: 1242: 1243:
1244: function get_link_class( $url_name ) {
1245: return isset( $this->_urls[$url_name]['link_class'] ) ? " class=\"{$this->_urls[$url_name]['link_class']}\"" : false;
1246: }
1247:
1248: 1249: 1250: 1251: 1252:
1253: function register_image( $image_name, $image_url, $args = array() ) {
1254: $args['image_name'] = $image_name;
1255: $args['image_url'] = $image_url;
1256: if ( ! isset( $args['url_vars'] ) )
1257: $args['image_vars'] = false;
1258: $this->_images[$image_name] = $args;
1259: }
1260:
1261: 1262: 1263: 1264: 1265:
1266: function has_image( $image_name ) {
1267: return isset( $this->_images[$image_name] );
1268: }
1269:
1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286:
1287: function get_image_url( $image_name, $values = array() ) {
1288: if ( ! is_array( $values ) ) {
1289: 1290: 1291:
1292: $values = func_get_args();
1293: array_shift( $values );
1294: }
1295: $image_url = $this->_images[$image_name]['image_url'];
1296: if ( is_array( $values ) ) {
1297: $vars = $this->_images[$image_name]['image_vars'];
1298: if ( ! $vars ) {
1299: preg_match_all( '#\{([^}]+)\}#', $image_url, $matches );
1300: $vars = $matches[1];
1301: }
1302: if ( is_numeric( key($values) ) ) {
1303: 1304: 1305:
1306: foreach( $values as $value ) {
1307: $image_url = str_replace( "{{$vars[0]}}", $value, $image_url );
1308: }
1309: } else {
1310: 1311: 1312:
1313: foreach( $vars as $name ) {
1314: $image_url = str_replace( "{{$name}}", $values[$name], $image_url );
1315: }
1316: }
1317: }
1318: if ( ! preg_match( '#^https?//#', $image_url ) ) {
1319: $image_url = plugins_url( "/images/{$image_url}", $this->plugin_file );
1320: }
1321: return $image_url;
1322: }
1323:
1324:
1325:
1326:
1327:
1328:
1329:
1330: 1331: 1332: 1333: 1334: 1335: 1336:
1337: function the_form( $form = false ) {
1338: if ( ! $form )
1339: $form = $this->get_current_form();
1340: return $form->the_form();
1341: }
1342:
1343: 1344: 1345: 1346:
1347: function promote_form( $form ) {
1348: 1349: 1350:
1351: $form_name = $form['form_name'];
1352: $form['plugin'] = $this;
1353:
1354: if ( ! isset( $form['admin_page'] ) )
1355: $form['admin_page'] = end( $this->_admin_pages );
1356:
1357: 1358: 1359:
1360: $form = $this->_forms[$form_name] = new Sidecar_Form( $form_name, $form );
1361:
1362: $this->set_current_form( $form );
1363: $this->initialize_form( $form );
1364: $form->initialize();
1365: return $form;
1366: }
1367: 1368: 1369:
1370: function the_form_section( $args ) {
1371: if ( ! empty( $args['section']->section_text ) )
1372: echo $args['section']->section_text;
1373: }
1374:
1375: 1376: 1377: 1378: 1379:
1380: function get_form_field( $field_name, $form_name ) {
1381: 1382: 1383:
1384: $form = $this->get_form( $form_name );
1385: return $form ? $form->get_field( $field_name ) : false;
1386: }
1387: 1388: 1389:
1390: function get_form_field_html( $field_name, $form_name ) {
1391: 1392: 1393:
1394: $field = $this->get_form_field( $field_name, $form_name );
1395: return $field->get_html();
1396: }
1397:
1398: 1399: 1400:
1401: function the_form_field( $field_name, $form_name ) {
1402: echo $this->get_form_field_html( $field_name, $form_name );
1403: }
1404:
1405: 1406: 1407: 1408:
1409: function has_shortcode( $shortcode_name ) {
1410: return isset( $this->_shortcodes[$shortcode_name] );
1411: }
1412: 1413: 1414: 1415:
1416: function get_form( $form ) {
1417: 1418: 1419:
1420: $form = is_string( $form ) && isset( $this->_forms[$form] ) ? $this->_forms[$form] : $form;
1421: if ( is_array( $form ) )
1422: $form = $this->promote_form( $form );
1423: return is_object( $form ) ? $form : false;
1424: }
1425: 1426: 1427: 1428:
1429: function add_form( $form_name, $args = array() ) {
1430: $args['form_name'] = $form_name;
1431: if ( ! isset( $args['requires_api'] ) && 'account' == $form_name )
1432: $args['requires_api'] = true;
1433: $this->_forms[$form_name] = $args;
1434: $this->_plugin_settings->register_form_settings( $form_name );
1435: }
1436:
1437: 1438: 1439: 1440:
1441:
1442: 1443: 1444: 1445: 1446:
1447: function add_field( $form_name, $args = array() ) {
1448: 1449: 1450:
1451: $form = isset( $args['form'] ) ? $args['form'] : end( $this->_forms );
1452: return $form->add_field( $form_name, $args );
1453: }
1454:
1455: 1456: 1457: 1458: 1459:
1460: function is_authenticated() {
1461: return $this->has_grant();
1462: }
1463:
1464: 1465: 1466: 1467: 1468:
1469: function has_grant() {
1470: $has_grant = false;
1471: if ( $this->needs_grant() ) {
1472: $auth_form_values = $this->get_auth_form()->get_settings_values();
1473: $has_grant = $this->get_api()->is_grant( $auth_form_values );
1474: }
1475: return $has_grant;
1476: }
1477:
1478: 1479: 1480:
1481: function needs_grant() {
1482: return $this->has_api();
1483: }
1484:
1485: 1486: 1487: 1488: 1489:
1490: function get_grant() {
1491: 1492: 1493:
1494: $auth_provider = $this->get_api()->get_auth_provider();
1495: return $auth_provider->extract_grant( $this->get_auth_form()->get_settings_values() );
1496: }
1497:
1498: 1499: 1500: 1501: 1502:
1503: function get_credentials() {
1504: 1505: 1506:
1507: $auth_provider = $this->get_api()->get_auth_provider();
1508: return $auth_provider->extract_credentials( $this->get_auth_form()->get_settings_values() );
1509: }
1510:
1511: 1512: 1513:
1514: function get_current_admin_page() {
1515: if ( ! isset( $this->_current_admin_page ) ) {
1516: if ( ! isset( $_GET['page'] ) || ! is_admin() ) {
1517: $this->_current_admin_page = false;
1518: } else {
1519: if ( isset( $_GET['tab'] ) ) {
1520: $tab = $_GET['tab'];
1521: } else {
1522: $page = $this->get_admin_page($_GET['page']);
1523: if ( $page && method_exists( $page, 'get_default_tab' ) ) {
1524: $tab = $page->get_default_tab()->tab_slug;
1525: }
1526: };
1527: 1528: 1529: 1530:
1531: foreach( array_keys( $this->_admin_pages ) as $admin_page_slug ) {
1532: if ( "{$this->plugin_slug}-{$admin_page_slug}" == $_GET['page'] ) {
1533: $this->_current_admin_page = $this->get_admin_page( $admin_page_slug );
1534: break;
1535: }
1536: }
1537: }
1538: }
1539: return $this->_current_admin_page;
1540: }
1541:
1542: 1543: 1544:
1545: function set_current_admin_page( $current_admin_page ) {
1546: $this->_current_admin_page = $current_admin_page;
1547: }
1548:
1549: 1550: 1551:
1552: function get_current_form() {
1553: return $this->_current_form;
1554: }
1555:
1556: 1557: 1558:
1559: function set_current_form( $current_form ) {
1560: $this->_current_form = $current_form;
1561: }
1562:
1563: 1564: 1565: 1566: 1567: 1568: 1569:
1570: function _pre_update_option( $new_value, $old_value ) {
1571: 1572: 1573: 1574: 1575: 1576: 1577: 1578: 1579: 1580: 1581:
1582: if ( ! isset( $new_value['_sidecar_form_meta'] ) ) {
1583: $return_value = $new_value;
1584: } else {
1585: $form_name = $new_value['_sidecar_form_meta']['form'];
1586: $this->set_form_settings( $form_name, $new_value[$form_name] );
1587: $return_value = $this->get_settings()->get_data();
1588: }
1589:
1590: return $return_value;
1591: }
1592: 1593: 1594: 1595:
1596: function _activate() {
1597: if ( ! $this->_initialized )
1598: $this->initialize();
1599:
1600: if ( method_exists( $this, 'activate' ) )
1601: $this->activate();
1602:
1603: global $wp_version;
1604: if ( version_compare( $wp_version, $this->min_wp, '<' ) ) {
1605: deactivate_plugins( basename( $this->plugin_file ) );
1606: $msg = __( 'Your site needs to be running WordPress %s or later in order to use %s.', 'sidecar' );
1607: trigger_error( sprintf( $msg, $this->min_wp, $this->plugin_title ), E_USER_ERROR );
1608: } if ( version_compare( PHP_VERSION, $this->min_php, '<' ) ) {
1609: deactivate_plugins( basename( $this->plugin_file ) );
1610: $msg = __( 'Your site needs to be running PHP %s or later in order to use %s.', 'sidecar' );
1611: trigger_error( sprintf( $msg, $this->min_php, $this->plugin_title ), E_USER_ERROR );
1612: } else {
1613:
1614:
1615:
1616:
1617: 1618: 1619: 1620:
1621: if ( $this->has_api() ) {
1622: $api = $this->get_api();
1623: 1624: 1625:
1626: $auth_provider = $api->get_auth_provider();
1627: $auth_form = $this->get_auth_form();
1628: if ( ! $auth_form )
1629: wp_die( __( 'There is no auth form configured. Call $admin_page->set_auth_form( $form_name ) inside initialize_admin_page( $admin_page ).', 'sidecar' ) );
1630:
1631: $account_settings = $auth_form->get_settings();
1632: $credentials = $auth_provider->extract_credentials( $account_settings->get_values() );
1633: $credentials = array_merge( $auth_provider->get_new_credentials(), $credentials );
1634: $credentials = $auth_provider->prepare_credentials( $credentials );
1635:
1636: 1637: 1638:
1639: if ( ! $auth_provider->is_credentials( $credentials ) ) {
1640: 1641: 1642: 1643:
1644: $grant = $auth_provider->prepare_grant( $auth_provider->get_new_grant(), $credentials );
1645:
1646: } else {
1647: 1648: 1649:
1650: $response = $api->authenticate( $credentials );
1651: 1652: 1653:
1654: $grant = $response->authenticated ? $response->grant : $auth_provider->get_new_grant();
1655:
1656: 1657: 1658: 1659:
1660: $grant = $auth_provider->prepare_grant( $grant, $credentials );
1661:
1662: }
1663: 1664: 1665:
1666: $account_settings->set_values( array_merge( $credentials, $grant ) );
1667:
1668: }
1669:
1670: $settings = $this->get_settings();
1671: $settings->installed_version = $this->plugin_version;
1672: $settings->update_settings();
1673:
1674: }
1675: }
1676:
1677: 1678: 1679:
1680: function _initialize_admin() {
1681: if ( ! $this->_admin_initialized ) {
1682: if ( ! method_exists( $this, 'initialize_admin' ) ) {
1683: trigger_error( __( 'Plugin must define a $plugin->initialize_admin() method..', 'sidecar' ) );
1684: exit;
1685: }
1686: $this->initialize_admin();
1687:
1688: $this->_admin_initialized = true;
1689:
1690: if ( $this->_api )
1691: $this->_api->maybe_set_grant( $this->get_grant() );
1692:
1693: }
1694: }
1695: 1696: 1697:
1698: function get_auth_form() {
1699: $this->_initialize_admin();
1700: $auth_form = false;
1701: 1702: 1703:
1704: foreach( $this->_admin_pages as $page ) {
1705: $this->initialize_admin_page( $page );
1706: if ( $test = $page->get_auth_form() ) {
1707: $auth_form = $test;
1708: break;
1709: }
1710: }
1711: return $auth_form;
1712: }
1713:
1714: 1715: 1716: 1717:
1718: function __get( $property_name ) {
1719: $value = false;
1720: if ( preg_match( '#^(.*?_(icon|image|photo))_url$#', $property_name, $match ) && $this->has_image( $match[1] ) ) {
1721: $value = call_user_func( array( $this, "get_image_url" ), $match[1] );
1722: } else if ( preg_match( '#^(.*?)_url$#', $property_name, $match ) && $this->has_url( $match[1] ) ) {
1723: 1724: 1725: 1726: 1727: 1728:
1729: $value = $this->get_url( $match[1] );
1730: } else if ( preg_match( '#^(.*?)_link$#', $property_name, $match ) && $this->has_url( $match[1] ) ) {
1731: 1732: 1733:
1734: $url = $this->get_url( $match[1] );
1735: $link_text = $this->get_link_text( $match[1] );
1736: $class = $this->get_link_class( $match[1] );
1737: $value = "<a target=\"_blank\"{$class} href=\"{$url}\">{$link_text}</A>";
1738: } else {
1739: Sidecar::show_error( 'No property named %s on %s class.', $property_name, get_class( $this ) );
1740: }
1741: return $value;
1742: }
1743: }
1744: