public static function instance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 public function __construct($attrs = array())
 {
     $defaults = array('id' => 0, 'label' => '', 'description' => '', 'field_type' => 'textfield', 'association' => 'meta', 'weight' => 0, 'validators' => array(), 'display_flags' => array(), 'field_data' => array());
     $attrs = wp_parse_args($attrs, $defaults);
     $formfields = WPBDP_FormFields::instance();
     $this->id = intval($attrs['id']);
     $this->label = $attrs['label'];
     $this->description = $attrs['description'];
     $this->type = is_object($attrs['field_type']) ? $attrs['field_type'] : WPBDP_FormFields::instance()->get_field_type($attrs['field_type']);
     if (!$this->type) {
         throw new Exception(_x('Invalid form field type', 'form-fields-api', 'WPBDM'));
     }
     /*        if ( !$this->type ) // temporary workaround related to 3.0 upgrade issues (issue #365)
               $this->type = WPBDP_FormFields::instance()->get_field_type( 'textfield' );*/
     $this->association = $attrs['association'];
     $this->weight = intval($attrs['weight']);
     /* Validators */
     if (is_array($attrs['validators'])) {
         foreach ($attrs['validators'] as $validator) {
             if ($validator && !in_array($validator, $this->validators, true)) {
                 $this->validators[] = $validator;
             }
         }
     }
     /* display_options */
     $this->display_flags = $attrs['display_flags'];
     $this->field_data = $attrs['field_data'];
     if ($this->association == 'category') {
         $this->field_data['options'] = array();
         // } elseif ( $this->association == 'category' ) {
         //     // TODO: make this hierarchical (see https://codex.wordpress.org/Function_Reference/Walker_Class)
         //     $terms = get_terms( $this->association == 'tags' ? WPBDP_TAGS_TAX : wpbdp_categories_taxonomy(), 'hide_empty=0&hierarchical=1' );
         //     $options = array();
         //     foreach ( $terms as &$term ) {
         //         $k = $this->association == 'tags' ? $term->slug : $term->term_id;
         //         $options [ $k ] = $term->name;
         //     }
         //     $this->field_data['options'] = $options;
     } else {
         // handle some special extra data from previous BD versions
         // TODO: this is not needed anymore since the 3.2 upgrade routine
         if (isset($attrs['field_data']) && isset($attrs['field_data']['options'])) {
             $options = array();
             foreach ($attrs['field_data']['options'] as $option_value) {
                 if (is_array($option_value)) {
                     $options[$option_value[0]] = $option_value[1];
                 } else {
                     $options[$option_value] = $option_value;
                 }
             }
             $this->field_data['options'] = $options;
         }
     }
     $this->type->setup_field($this);
     do_action_ref_array('wpbdp_form_field_setup', array(&$this));
 }
 function init()
 {
     $this->compat = new WPBDP_Compat();
     // Register cache groups.
     wp_cache_add_non_persistent_groups(array('wpbdp pages', 'wpbdp formfields', 'wpbdp submit state', 'wpbdp'));
     // Register some basic JS resources.
     add_action('wp_enqueue_scripts', array(&$this, 'register_common_scripts'));
     add_action('admin_enqueue_scripts', array(&$this, 'register_common_scripts'));
     // Initialize settings API.
     $this->settings = new WPBDP_Settings();
     $this->formfields = WPBDP_FormFields::instance();
     // Install plugin.
     $this->settings->register_settings();
     $this->_register_post_type();
     $this->install_or_update_plugin();
     if ($manual_upgrade = get_option('wpbdp-manual-upgrade-pending', false)) {
         $installer = new WPBDP_Installer();
         $installer->setup_manual_upgrade();
         return;
     }
     // Display "Settings" link on Plugins page.
     $plugin_filename = plugin_basename(__FILE__);
     add_filter('plugin_action_links_' . $plugin_filename, array(&$this, 'plugin_action_links'));
     // Initialize APIs.
     $this->admin = is_admin() ? new WPBDP_Admin() : null;
     $this->controller = new WPBDP_DirectoryController();
     $this->fees = new WPBDP_FeesAPI();
     $this->payments = new WPBDP_PaymentsAPI();
     $this->listings = new WPBDP_Listings_API();
     $this->_register_image_sizes();
     add_filter('posts_request', create_function('$x', 'wpbdp_debug($x); return $x;'));
     // used for debugging
     add_filter('rewrite_rules_array', array(&$this, '_rewrite_rules'));
     add_filter('query_vars', array(&$this, '_query_vars'));
     add_filter('redirect_canonical', array(&$this, '_redirect_canonical'), 10, 2);
     add_action('template_redirect', array(&$this, '_template_redirect'));
     add_action('wp_loaded', array(&$this, '_wp_loaded'));
     add_action('save_post_page', array(&$this, '_invalidate_pages_cache'));
     add_action('pre_get_posts', array(&$this, '_pre_get_posts'));
     add_filter('posts_clauses', array(&$this, '_posts_clauses'), 10, 2);
     add_filter('posts_fields', array(&$this, '_posts_fields'), 10, 2);
     add_filter('posts_orderby', array(&$this, '_posts_orderby'), 10, 2);
     add_filter('comments_template', array(&$this, '_comments_template'));
     add_filter('taxonomy_template', array(&$this, '_category_template'));
     add_filter('single_template', array(&$this, '_single_template'));
     add_action('wp', array(&$this, '_meta_setup'));
     add_action('wp', array(&$this, '_jetpack_compat'), 11, 1);
     add_action('wp_head', array(&$this, '_handle_broken_plugin_filters'), 0);
     add_filter('wp_title', array(&$this, '_meta_title'), 10, 3);
     add_action('wp_head', array(&$this, '_rss_feed'));
     // Register shortcodes.
     $shortcodes = $this->get_shortcodes();
     foreach ($shortcodes as $shortcode => &$handler) {
         add_shortcode($shortcode, $handler);
     }
     do_action('wpbdp_loaded');
     // Expiration hook.
     add_action('wpbdp_listings_expiration_check', array(&$this, '_notify_expiring_listings'), 0);
     // Scripts & styles.
     add_action('wp_enqueue_scripts', array($this, '_enqueue_scripts'));
     add_action('wp_enqueue_scripts', array(&$this, 'enqueue_css_override'), 30, 0);
     // Plugin modules initialization.
     $this->_init_modules();
     // AJAX actions.
     add_action('wp_ajax_wpbdp-file-field-upload', array(&$this, 'ajax_file_field_upload'));
     add_action('wp_ajax_nopriv_wpbdp-file-field-upload', array(&$this, 'ajax_file_field_upload'));
     add_action('wp_ajax_wpbdp-listing-submit-image-upload', array(&$this, 'ajax_listing_submit_image_upload'));
     add_action('wp_ajax_nopriv_wpbdp-listing-submit-image-upload', array(&$this, 'ajax_listing_submit_image_upload'));
     add_action('wp_ajax_wpbdp-listing-submit-image-delete', array(&$this, 'ajax_listing_submit_image_delete'));
     add_action('wp_ajax_nopriv_wpbdp-listing-submit-image-delete', array(&$this, 'ajax_listing_submit_image_delete'));
     // Core sorting options.
     add_filter('wpbdp_listing_sort_options', array(&$this, 'sortbar_sort_options'));
     add_filter('wpbdp_query_fields', array(&$this, 'sortbar_query_fields'));
     add_filter('wpbdp_query_orderby', array(&$this, 'sortbar_orderby'));
     $this->recaptcha = new WPBDP_reCAPTCHA();
 }
 private function processFieldForm()
 {
     $api = WPBDP_FormFields::instance();
     if (isset($_POST['field'])) {
         $field = new WPBDP_FormField(stripslashes_deep($_POST['field']));
         $res = $field->save();
         if (!is_wp_error($res)) {
             $this->admin->messages[] = _x('Form fields updated.', 'form-fields admin', 'WPBDM');
             return $this->fieldsTable();
         } else {
             $errmsg = '';
             foreach ($res->get_error_messages() as $err) {
                 $errmsg .= sprintf('&#149; %s<br />', $err);
             }
             $this->admin->messages[] = array($errmsg, 'error');
         }
     } else {
         $field = isset($_GET['id']) ? WPBDP_FormField::get($_GET['id']) : new WPBDP_FormField(array('display_flags' => array('excerpt', 'search', 'listing')));
     }
     wpbdp_render_page(WPBDP_PATH . 'admin/templates/form-fields-addoredit.tpl.php', array('field' => $field, 'field_associations' => $api->get_associations_with_flags(), 'field_types' => $api->get_field_types(), 'validators' => $api->get_validators(), 'association_field_types' => $api->get_association_field_types()), true);
 }