/** * @group shortcode * @see gravityview_has_shortcode_r * @covers GVCommon::has_shortcode_r * @covers ::gravityview_has_shortcode_r() */ function test_gravityview_has_shortcode_r() { $single_view = array('[gravityview id=123]', null, 'gravityview', ' id=123', null, null, null); $shortcodes = gravityview_has_shortcode_r('[gravityview id=123]'); $this->assertEquals($shortcodes, array($single_view), 'failed unnested test'); $shortcodes = gravityview_has_shortcode_r('[gravityview id=123][gravityview id=123]'); $this->assertEquals($shortcodes, array($single_view, $single_view), 'unnested two shortcodes test'); $shortcodes = gravityview_has_shortcode_r('[example][gravityview id=123][example2][gravityview id=123][example3][gravityview id=123][/example3][/example2][/example]'); $this->assertEquals($shortcodes, array($single_view, $single_view, $single_view), 'nested three shortcodes'); }
/** * Read the $post and process the View data inside * @param array $wp Passed in the `wp` hook. Not used. * @return void */ function parse_content($wp = array()) { global $post; // If in admin and NOT AJAX request, get outta here. if (GravityView_Plugin::is_admin()) { return; } // Calculate requested Views $this->setGvOutputData(GravityView_View_Data::getInstance($post)); // !important: we need to run this before getting single entry (to kick the advanced filter) $this->set_context_view_id(); $this->setIsGravityviewPostType(get_post_type($post) === 'gravityview'); $post_id = $this->getPostId() ? $this->getPostId() : (isset($post) ? $post->ID : null); $this->setPostId($post_id); $post_has_shortcode = !empty($post->post_content) ? gravityview_has_shortcode_r($post->post_content, 'gravityview') : false; $this->setPostHasShortcode($this->isGravityviewPostType() ? null : !empty($post_has_shortcode)); // check if the View is showing search results (only for multiple entries View) $this->setIsSearch($this->is_searching()); unset($entry, $post_id, $post_has_shortcode); }
/** * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head. * * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively) * @uses shortcode_parse_atts() Parse each GV shortcode * @uses gravityview_get_template_settings() Get the settings for the View ID * @param string $content $post->post_content content * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed. */ function parse_post_content($content) { /** * @hack This is so that the shortcode is registered for the oEmbed preview in the Admin * @since 1.6 */ if (!shortcode_exists('gravityview')) { add_shortcode('gravityview', array(GravityView_frontend::getInstance(), 'shortcode')); } $shortcodes = gravityview_has_shortcode_r($content, 'gravityview'); if (empty($shortcodes)) { return NULL; } do_action('gravityview_log_debug', 'GravityView_View_Data[parse_post_content] Parsing content, found shortcodes:', $shortcodes); $ids = array(); foreach ($shortcodes as $key => $shortcode) { $args = shortcode_parse_atts($shortcode[3]); if (empty($args['id'])) { do_action('gravityview_log_error', sprintf('GravityView_View_Data[parse_post_content] Returning; no ID defined in shortcode atts for Post #%s (Atts)', $post->ID), $shortcode); continue; } do_action('gravityview_log_debug', sprintf('GravityView_View_Data[parse_post_content] Adding view #%s with shortcode args', $args['id']), $args); // Store the View to the object for later fetching. $this->add_view($args['id'], $args); $ids[] = $args['id']; } if (empty($ids)) { return NULL; } // If it's just one ID, return that. // Otherwise, return array of IDs return sizeof($ids) === 1 ? $ids[0] : $ids; }