/** * Run this plugin, load textdomain, adds filters & actions * * @return void */ public function initialize() { // Load textdomain load_plugin_textdomain('tea-page-content', false, TEA_PAGE_CONTENT_FOLDER . '/languages/'); // Adds filters, actions, etc. add_action('init', array($this, 'registerShortcodes')); add_action('widgets_init', array($this, 'registerWidgets')); // Modify Admin page add_action('admin_footer-widgets.php', array($this, 'addPageVariablesModal')); add_action('admin_footer-edit.php', array($this, 'addPageVariablesModal')); add_action('admin_footer-post.php', array($this, 'addPageVariablesModal')); add_action('admin_footer-post-new.php', array($this, 'addPageVariablesModal')); add_action('admin_footer-edit.php', array($this, 'addInsertShortcodeModal')); add_action('admin_footer-post.php', array($this, 'addInsertShortcodeModal')); add_action('admin_footer-post-new.php', array($this, 'addInsertShortcodeModal')); // Includes all css, js, etc. add_action('wp_enqueue_scripts', array($this, 'includeAssets'), 100, 1); add_action('admin_enqueue_scripts', array($this, 'includeAdminAssets'), 100, 1); // Set Callbacks for ajax actions add_action('wp_ajax_get_template_variables', array($this, 'getTemplateVariablesCallback')); add_action('wp_ajax_generate_shortcode', array($this, 'generateShortcode')); add_action('wp_ajax_set_notice_seen', array($this, 'setNoticeSeenCallback')); add_action('media_buttons', array($this, 'add_my_media_button'), 1000); // At first time notice user about possible migration if (!get_option('tpc_deprecated_notice')) { add_action('admin_notices', array($this, 'displayDeprecatedNotice')); } // Create new helper instance $this->_helper = new TeaPageContent_Helper(); // Gets instance of the config class $this->_config = TeaPageContent_Config::getInstance(); }
/** * Main constructor, set up all global preferenses * and parameters, e.g. description, title, etc. * * @return void */ public function __construct() { parent::__construct('tea-page-content', __('Tea Page Content', 'tea-page-content'), array('description' => __('Allows display any content of any page or post.', 'tea-page-content')), array('width' => 480)); // Get helper object $this->_helper = new TeaPageContent_Helper(); // And config instance $this->_config = TeaPageContent_Config::getInstance(); // Set defaults $this->params = $this->_config->get('defaults.widget', array('per-page', 'caller')); }
/** * Main shortcode for this plugin * Returns html code of rendered shortcode, * or false if output is empty * * @param array $userAttrs * @param string|null $content * @return mixed */ public static function tea_page_content($userAttrs, $content = null) { $output = false; $helper = new TeaPageContent_Helper(); $config = TeaPageContent_Config::getInstance(); // Shortcode defaults $defaults = $config->get('defaults.shortcode'); $attrs = array(); foreach ($defaults as $attr_title => $attr_value) { if ($attr_title === 'caller') { // @todo не очень явно, магическое слово, нужно как-то вынести в конфиг или еще куда continue; } if (isset($userAttrs[$attr_title])) { $attrs[$attr_title] = $attr_value; } else { $attrs[$attr_title] = null; } } // Merge existing attrs and user attrs $attrs = shortcode_atts($attrs, $userAttrs); // Get template variables for chosen template $template_variables = $helper->getVariables($attrs['template']); // If we have content, it means than user want set page level vars if (!is_null($content) && trim($content)) { // Here we will store all parsed pieces of inner shortcodes // Because we need render it in single layout $innerAttrsBundle = array(); $params = array(); // Split content data by shortcodes $content = preg_split('/(\\[tea_page_content.*\\])/i', $content, null, PREG_SPLIT_DELIM_CAPTURE); // And filter it for excluding trash data $content = array_filter($content, function ($elem) { if (preg_match('/^\\[tea_page_content/i', $elem)) { return true; } return false; }); // Get the entries for every piece $params['entries'] = array(); $entries_list = array(); $pageVariables = array(); // Then, grep parsed data step by step foreach ($content as $index => $shortcode) { // Split every piece to params, with two separate groups for title and value $current = preg_split('/(\\w+)=\\"([^"]+)\\"/ui', $shortcode, null, PREG_SPLIT_DELIM_CAPTURE); $innerAttrsBundle[$index] = array(); // Now combine title and value into one param for ($i = 0, $count = count($current); $i < $count; $i++) { $attr = $current[$i]; // Check for non-page-level variables, if it exists, skip two iterations if ($attr !== 'posts' && in_array($attr, array_keys($defaults))) { $i++; continue; } // Combine only if current piece is really param... if ($i < $count - 1 && preg_match('/^[^\\[][\\w\\d]+/ui', $attr)) { $nextAttr = $current[$i + 1]; $innerAttrsBundle[$index][$attr] = $nextAttr; $i++; } } $innerAttrs = $innerAttrsBundle[$index]; if (!array_key_exists('posts', $innerAttrs)) { continue; } $current_posts = explode(',', $innerAttrs['posts']); foreach ($current_posts as $current_post_id) { $pageVariables[(int) $current_post_id] = $helper->extractPageVariables($innerAttrs); } // make dis shit dry {3} $entries_list = array_merge($entries_list, $current_posts); } $params['entries'] = $helper->getPosts(array('post__in' => $entries_list, 'order' => $attrs['order']), 'flatten'); foreach ($params['entries'] as &$entry) { // Then we can merge it with original entry. // By default, original values in entry will be OVERRIDE, // and if you want change this behavior, // you can use filter `tpc_get_page_variables` $entry = array_merge($entry, $helper->preparePageVariablesForMerge($pageVariables[$entry['id']])); } unset($entry); $params['count'] = count($params['entries']); $params = array_merge($params, $helper->getParams($attrs, 'flatten')); } else { // If we haven't content. It means that this is usual shortcode // Pre-create template variables, if exists // @todo вынести в отдельную функцию, be DRY $attrs['template_variables'] = array(); foreach ($template_variables as $variable => $value) { if ($value['type'] === 'caption') { continue; } if (isset($userAttrs[$variable])) { $attrs['template_variables'][$variable] = $userAttrs[$variable]; } else { switch ($value['type']) { case 'checkbox': if (reset($value['defaults'])) { $attrs['template_variables'][$variable] = $variable; } break; default: $attrs['template_variables'][$variable] = reset($value['defaults']); break; } } } // Gets params for this shortcode $params = $helper->getParams($attrs, 'flatten'); } // If params is not empty render shortcode if (!empty($attrs) && !empty($params['entries'])) { $templatePath = $helper->getTemplatePath($attrs['template']); $output = $helper->renderTemplate($params, $templatePath); } return $output; }
/** * Constructor * * @return void */ public function __construct() { // Gets instance of config object $this->_config = TeaPageContent_Config::getInstance(); }