Example #1
0
 public static function doOnSuccess()
 {
     if (!self::$form_conf_id) {
         return;
     }
     if (!is_numeric(self::$form_conf_id)) {
         return;
     }
     $form_config = \Taco\Post::find(self::$form_conf_id);
     if (!\AppLibrary\Obj::iterable($form_config)) {
         return;
     }
     $on_success = $form_config->get('on_success');
     if (!is_callable($on_success)) {
         return;
     }
     $method_class = explode('::', $on_success)[0];
     $method = explode('::', $on_success)[1];
     return $method_class::$method(self::$record, $form_config);
 }
 /**
  * Save
  * @param bool $exclude_post
  * @return bool
  */
 public function save($exclude_post = false)
 {
     // Only one theme option configuration can be active
     if ($this->get(self::KEY_IS_ACTIVE)) {
         $instance = self::getInstance();
         if ($instance->get('ID') && $instance->get('ID') !== $this->get('ID')) {
             $instance->set(self::KEY_IS_ACTIVE, false);
             $instance->save(true);
             // Passing true to avoid recursion
         }
     }
     return parent::save($exclude_post);
 }
Example #3
0
 /**
  * creates a new MrSpicy and associated configuration
  * @param  $args array
  * @param  $template_callback callable
  * @return MrSpicy object
  */
 public function __construct($args, $template_callback = null)
 {
     // set submit action uri
     self::setSubmitActionURI();
     $defaults = array('conf_name' => '', 'fields' => array(), 'css_class' => '', 'id' => '', 'method' => 'post', 'action' => null, 'novalidate' => false, 'hide_labels' => false, 'column_classes' => 'small-12 columns', 'exclude_post_content' => false, 'lock' => false, 'submit_button_text' => 'submit', 'success_message' => null, 'error_message' => null, 'success_redirect_url' => null, 'label_field_wrapper' => '\\Taco\\MrSpicy::rowColumnWrap', 'on_success' => null, 'use_honeypot' => false, 'honeypot_field_name' => 'your_webite_url', 'test_with_fakes' => false, 'use_recaptcha' => false, 'google_recaptcha_site_key' => null, 'google_recaptcha_secret_key' => null);
     // we need this to uniquely identify the form conf that will get created or loaded
     if (!(array_key_exists('conf_name', $args) && strlen($args['conf_name']))) {
         throw new Exception('conf_name must be defined in the args array');
         exit;
     }
     // if the form configuration exists, load it
     $db_conf = $this->findFormConfigInstance($args['conf_name']);
     if ($db_conf) {
         $this->conf_instance = $db_conf;
     } else {
         $this->conf_instance = new \FormConfig();
         $this->conf_instance->set('unique_id', md5($args['conf_name']));
     }
     $conf_fields = $this->conf_instance->getFields();
     // load global defaults
     // TODO: find a better way than using a global
     global $mr_spicy_forms_defaults_path;
     if (strlen($mr_spicy_forms_defaults_path)) {
         $global_defaults = (include $mr_spicy_forms_defaults_path);
     } else {
         $global_defaults = (include __DIR__ . '/../forms-defaults.php');
     }
     // get the default form action
     $defaults['action'] = array_key_exists('form_action', $global_defaults) && !is_null($global_defaults['form_action']) ? $global_defaults['form_action'] : self::getSubmitActionURI();
     // assign only the fields specified above and in the form conf
     foreach ($args as $k => $v) {
         if ($k === 'fields' && $args['fields'] !== 'auto') {
             $this->fields = $v;
             continue;
         }
         if ($k == 'on_success') {
             continue;
         }
         if (array_key_exists($k, $conf_fields)) {
             $this->conf_instance->set($args[$k], $args[$k]);
         }
         if (array_key_exists($k, $defaults)) {
             $this->settings[$k] = $args[$k];
         }
     }
     /* We cannot use a closure with an event callback
      *  where a redirect url is defined
      * Instead we must use a string
      */
     if (!is_object($this->settings['on_success']) && strlen($this->settings['on_success']) && strlen($this->settings['success_redirect_url'])) {
         $this->conf_instance->set('on_success', $this->settings['on_success']);
     } else {
         $this->conf_instance->set('on_success', null);
         if (!strlen($this->conf_instance->get('success_redirect_url'))) {
             $this->conf_instance->set('success_redirect_url', null);
         }
     }
     // if a callback is defined call it on success
     if (self::$success === true) {
         if ($args['on_success'] && !strlen($this->settings['success_redirect_url'])) {
             $taco_object = \Taco\Post::find(self::$entry_id);
             if (is_string($args['on_success'])) {
                 // is it a string?
                 $class_and_method = explode('::', $args['on_success']);
                 $method_class = $class_and_method[0];
                 $class_method = $class_and_method[1];
                 $method_class::$class_method($taco_object, $this);
             } else {
                 // it's a closure
                 $args['on_success']($taco_object, $this);
             }
         }
     }
     // --- messages ---
     // first get global default messages
     $defaults['success_message'] = $global_defaults['success_message'];
     $defaults['error_message'] = $global_defaults['error_message'];
     // second get developer's hardcoded per form settings from $args
     if (array_key_exists('success_message', $this->settings)) {
         $defaults['success_message'] = $this->settings['success_message'];
     }
     if (array_key_exists('error_message', $this->settings)) {
         $defaults['error_message'] = $this->settings['error_message'];
     }
     // lastly use the WordPress admin's message settings
     if (strlen($this->conf_instance->get('form_success_message'))) {
         $this->settings['success_message'] = $this->conf_instance->get('form_success_message');
     }
     if (strlen($this->conf_instance->get('form_error_message'))) {
         $this->settings['error_message'] = $this->conf_instance->get('form_error_message');
     }
     // merge default settings with user settings
     $this->settings = array_merge($defaults, $this->settings);
     // wrapper label/field method (is it the default, and is it a method or func?)
     if (is_string($this->settings['label_field_wrapper'])) {
         $wrapper_callable = explode('::', $this->settings['label_field_wrapper']);
         if (count($wrapper_method) > 1) {
             $wrapper_callable = current($wrapper_callable);
         }
         $this->settings['label_field_wrapper'] = $wrapper_callable;
     }
     // assign post title to instance
     $this->conf_instance->set('post_title', $this->get('conf_name'));
     if ($this->settings['fields'] !== 'auto') {
         $this->conf_instance->set('fields', serialize($this->fields));
     }
     // don't use a closure if the success_redirect_url is used
     if (strlen($this->settings['success_redirect_url']) && is_string($args['on_success'])) {
         $this->conf_instance->set('on_success', $args['on_success']);
     }
     // throw an error if settings includes a success_redirect_url with a closure
     if (!is_string($args['on_success']) && strlen($this->settings['success_redirect_url'])) {
         throw new Exception('MrSpicy: If you are using "success_redirect_url", you cannot use a closure.
       Use must specifiy valid string callback e.g. "MyClass::myMethod"', 1);
     }
     // assign conf machine name
     $this->conf_machine_name = \AppLibrary\Str::machine($this->get('conf_name'), '-');
     $this->conf_instance->assign(['use_honeypot' => $this->settings['use_honeypot'], 'honeypot_field_name' => $this->settings['honeypot_field_name']]);
     $this->conf_instance->set('use_recaptcha', $this->settings['use_recaptcha']);
     $this->conf_instance->set('google_recaptcha_site_key', $this->settings['google_recaptcha_site_key']);
     $this->conf_instance->set('google_recaptcha_secret_key', $this->settings['google_recaptcha_secret_key']);
     $this->conf_instance->set('post_name', $this->conf_machine_name);
     // assign redirect url from dev's settings
     // if the admin adds this value, it will need to be overridden from wp-admin
     if (!strlen($this->conf_instance->get('success_redirect_url'))) {
         $this->conf_instance->set('success_redirect_url', $this->settings['success_redirect_url']);
     }
     /* Do not save settings if locked (prevents extra db/backend work)
      * Checking for the prod environment could
      *  be one way of automatically turning the lock on or off
      */
     if (!($this->settings['lock'] && $this->conf_instance->get('ID'))) {
         // if the entry doesn't exist create it in the db
         $this->conf_ID = $this->conf_instance->save();
         // get the updated form conf after save
         $this->conf_instance = \FormConfig::find($this->conf_ID);
     }
     return $this;
 }
Example #4
0
 public function getPostTypeConfig()
 {
     return array_merge(parent::getPostTypeConfig(), array('show_in_menu' => 'edit.php?post_type=form-config'));
 }
Example #5
0
 public static function getSubPosts($fieldname, $post_id)
 {
     $record = \Taco\Post::find($post_id);
     $field_ids = explode(',', $record->get($fieldname));
     $subposts = self::getSubPostsSafe($fieldname, $post_id);
     $subpost_ids = Collection::pluck($subposts, 'ID');
     $filtered = [];
     foreach ($field_ids as $fid) {
         if (!in_array($fid, $subpost_ids)) {
             continue;
         }
         $filtered[$fid] = \Taco\Post::find($fid);
     }
     return $filtered;
 }
 /**
  * Hide Theme Options Base from the admin menu
  */
 public function getPostTypeConfig()
 {
     return $this->getPostType() === 'theme-opt-base' ? null : parent::getPostTypeConfig();
 }
 /**
  * Get breadcrumbs HTML
  * @param \Taco\Post $post_obj
  * @return string HTML
  */
 public static function getBreadcrumbs(\Taco\Post $post_obj)
 {
     $separator = ' » ';
     $post_id = $post_obj->get('ID');
     $post_type = $post_obj->getPostType();
     $ancestor_links = array();
     $post_title = null;
     if (is_archive()) {
         // This is the blog archive, what to do for breadcrumbs here?
         return null;
     }
     if ($post_type == 'page') {
         $post_title = $post_obj->getTheTitle();
         $ancestors = get_post_ancestors($post_id);
         if (Arr::iterable($ancestors)) {
             $ancestors = array_reverse($ancestors);
             foreach ($ancestors as $ancestor_post_id) {
                 $ancestor = \Taco\Post\Factory::create($ancestor_post_id, false);
                 $single_post = get_post($post_id);
                 $ancestor_links[] = sprintf('<li><a href="%s">%s</a></li>', $ancestor->getPermalink(), $ancestor->getTheTitle());
             }
         }
     } elseif ($post_type == 'article') {
         $ancestor_links[] = '<li><a href="' . URL_NEWS . '">News</a></li>';
         $topics = $post_obj->getTerms('topic');
         if (Arr::iterable($topics)) {
             $topic = reset($topics);
             $ancestor_links[] = sprintf('<li><a href="%s">%s</a></li>', $topic->getPermalink(), $topic->get('name'));
         }
     }
     // Don't display breadcrumbs unless there's at least one ancestor
     if (!Arr::iterable($ancestor_links)) {
         return null;
     }
     return sprintf('<ul class="bread-crumbs">
     %s
     <li>%s</li>
   </ul>', join('', $ancestor_links), $post_title);
 }