From fbee3ac32e74582dd1e68521611c134f3f8d08c0 Mon Sep 17 00:00:00 2001 From: Scott Limmer Date: Mon, 13 Jun 2022 13:01:14 +1000 Subject: [PATCH 1/3] Fix "PHP Fatal error: Uncaught TypeError: Unsupported operand types: string - int" --- functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index 7a18042..af49541 100644 --- a/functions.php +++ b/functions.php @@ -233,8 +233,8 @@ function list_news($attrs) 'category' => 'News' ], $attrs); - $page = get_query_var('paged'); - $page = $page == 0 ? 1 : $page; + $page = filter_var(get_query_var('paged'), FILTER_VALIDATE_INT); + $page = empty($page) ? 1 : $page; $per_page = 10; $news_args = [ From 05afb7a97999998b9f4c4771f0f415b6ad17027d Mon Sep 17 00:00:00 2001 From: Scott Limmer Date: Mon, 13 Jun 2022 13:02:07 +1000 Subject: [PATCH 2/3] cleanup functions.php --- functions.php | 347 +++++++++++++++----------------------------------- 1 file changed, 101 insertions(+), 246 deletions(-) diff --git a/functions.php b/functions.php index af49541..a1dd508 100644 --- a/functions.php +++ b/functions.php @@ -23,7 +23,6 @@ function vwsg_styles() } } - add_action('wp_enqueue_scripts', 'vwsg_styles'); // Enqueue scripts @@ -50,14 +49,12 @@ function vwsg_setup() add_action('after_setup_theme', 'vwsg_setup'); - // Remove wordpress generator meta add_filter('the_generator', function () { return ''; }); remove_action('wp_head', 'wp_generator'); - add_filter('pre_get_posts', function ($query) { if ($query->is_search === true) { $query->set('post_type', ['post', 'page', 'attachment']); @@ -80,7 +77,6 @@ function add_categories_for_attachments() add_action('init', 'add_categories_for_attachments'); - // Enable tags for attachments function add_tags_for_attachments() { @@ -89,7 +85,6 @@ function add_tags_for_attachments() add_action('init', 'add_tags_for_attachments'); - function wpdocs_excerpt_more(): string { return '...'; @@ -97,10 +92,10 @@ function wpdocs_excerpt_more(): string add_filter('excerpt_more', 'wpdocs_excerpt_more'); - /** * Is the current post a single post or a post listing. * In the context of this theme, is it news + * * @return bool */ function is_news(): bool @@ -116,7 +111,6 @@ function get_breadcrumb() get_template_part('breadcrumb'); } - /** * Utility function to retrieve News & Events page * @@ -127,7 +121,6 @@ function get_news_page() return get_post(17); } - function build_tweet($tweet, $user_template): string { $twitter_user = 'vwsg_web'; @@ -147,7 +140,6 @@ function build_tweet($tweet, $user_template): string $text = $tweet['retweeted_status']['full_text']; $is_retweet = true; } else { - $text = $tweet['full_text']; } foreach ($tweet['entities']['user_mentions'] as $i => $user_mention) { @@ -156,7 +148,8 @@ function build_tweet($tweet, $user_template): string } $text = str_replace( '@' . $user_mention['screen_name'], - sprintf('@%s', + sprintf( + '@%s', 'https://twitter.com/' . $user_mention['screen_name'], $user_mention['screen_name'] ), @@ -179,7 +172,8 @@ function build_tweet($tweet, $user_template): string if ($is_retweet) : foreach ($tweet['retweeted_status']['entities']['media'] as $media) { $text = str_replace( - $media, '', + $media, + '', $text ); } @@ -192,7 +186,6 @@ function build_tweet($tweet, $user_template): string '' ); - return sprintf( $template, $text, @@ -206,23 +199,20 @@ function build_tweet($tweet, $user_template): string */ function display_tweet($template = '') { - $json = json_decode(file_get_contents(get_template_directory() . '/cron/vwsg_web.json'), JSON_OBJECT_AS_ARRAY); $tweet = $json[0]; echo build_tweet($tweet, $template); - } function display_tweets($template = '') { $json = json_decode(file_get_contents(get_template_directory() . '/cron/vwsg_web.json'), JSON_OBJECT_AS_ARRAY); - foreach ($json as $i => $tweet) { - if ($i > 2) { - break; - } + if ($i > 2) { + break; + } echo build_tweet($tweet, $template); } } @@ -233,14 +223,14 @@ function list_news($attrs) 'category' => 'News' ], $attrs); - $page = filter_var(get_query_var('paged'), FILTER_VALIDATE_INT); - $page = empty($page) ? 1 : $page; - $per_page = 10; + $page = filter_var(get_query_var('paged'), FILTER_VALIDATE_INT); + $page = empty($page) ? 1 : $page; + $per_page = 10; $news_args = [ 'post_type' => 'post', - 'posts_per_page' => $per_page, - 'offset' => ($page - 1) * $per_page, + 'posts_per_page' => $per_page, + 'offset' => ($page - 1) * $per_page, ]; switch ($attrs['category']) { @@ -258,21 +248,15 @@ function list_news($attrs) break; default: $news_args['cat'] = get_cat_ID($attrs['category']); - } - ob_start(); $query = new WP_Query($news_args); if ($query->have_posts()) { while ($query->have_posts()) : $query->the_post(); - /* if ($compact) { - get_template_part('partial/news-item-compact'); - } else {*/ - get_template_part('partial/news-item'); -// } + get_template_part('partial/news-item'); endwhile; } @@ -281,20 +265,22 @@ function list_news($attrs) ?> - 'page', @@ -311,21 +298,19 @@ function list_subpages($attrs) { 'orderby' => ['menu_order', 'post_title'] ]; - - ob_start(); $query = new WP_Query($subpage_args); if ($query->have_posts()) { ?> -
- have_posts()) : - $query->the_post(); +
+ have_posts()) : + $query->the_post(); - get_template_part('partial/subpage-listing-item'); + get_template_part('partial/subpage-listing-item'); - endwhile; ?> -
- +
+ have_posts()) : $query->the_post(); $count++; ?> -
-
-

-
-
- -
-
+
+
+

+
+
+ +
+
post_parent) { - $parent_post = get_post($parent_post->post_parent); - } - return $parent_post; +function get_top_ancestor() +{ + $parent_post = get_post(); + while ($parent_post->post_parent) { + $parent_post = get_post($parent_post->post_parent); + } + + return $parent_post; } -function get_child_pages($parent_post) { - return get_pages(['child_of' => $parent_post->ID, 'sort_column' => 'menu_order,post_title']); +function get_child_pages($parent_post) +{ + return get_pages(['child_of' => $parent_post->ID, 'sort_column' => 'menu_order,post_title']); } class Breadcrumb_Walker extends Walker_Nav_Menu @@ -392,13 +381,13 @@ protected function get_direct_ancestors($elements): array $ancestors = []; do { - $ancestor = array_filter($elements, function ($item) use ($currentID) { - if ($item->ID == $currentID || $item->object_id == $currentID) { - return true; - } else { - return false; - } - }); + $ancestor = array_filter($elements, function ($item) use ($currentID) { + if ($item->ID == $currentID || $item->object_id == $currentID) { + return true; + } else { + return false; + } + }); $ancestor = array_pop($ancestor); $currentID = $ancestor->post_parent; array_unshift($ancestors, $ancestor); @@ -431,31 +420,30 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) if ($item->object_id == get_the_ID()) { $output .= ' > ' . $item->title; } else { - $classes = empty($item->classes) ? array() : (array)$item->classes; $classes[] = 'menu-item-' . $item->ID; /** * Filters the arguments for a single nav menu item. * - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param WP_Post $item Menu item data object. - * @param int $depth Depth of menu item. Used for padding. - * @since 4.4.0 + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param WP_Post $item Menu item data object. + * @param int $depth Depth of menu item. Used for padding. * + * @since 4.4.0 */ $args = apply_filters('nav_menu_item_args', $args, $item, $depth); /** * Filters the CSS class(es) applied to a menu item's list item element. * - * @param array $classes The CSS classes that are applied to the menu item's `
  • ` element. - * @param WP_Post $item The current menu item. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param int $depth Depth of menu item. Used for padding. + * @param array $classes The CSS classes that are applied to the menu item's `
  • ` element. + * @param WP_Post $item The current menu item. + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param int $depth Depth of menu item. Used for padding. + * * @since 3.0.0 * @since 4.1.0 The `$depth` parameter was added. - * */ $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth)); $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; @@ -463,13 +451,13 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) /** * Filters the ID applied to a menu item's list item element. * - * @param string $menu_id The ID that is applied to the menu item's `
  • ` element. - * @param WP_Post $item The current menu item. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param int $depth Depth of menu item. Used for padding. + * @param string $menu_id The ID that is applied to the menu item's `
  • ` element. + * @param WP_Post $item The current menu item. + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param int $depth Depth of menu item. Used for padding. + * * @since 3.0.1 * @since 4.1.0 The `$depth` parameter was added. - * */ $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth); $id = $id ? ' id="' . esc_attr($id) . '"' : ''; @@ -485,20 +473,21 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) /** * Filters the HTML attributes applied to a menu item's anchor element. * - * @param array $atts { - * The HTML attributes applied to the menu item's `` element, empty strings are ignored. + * @param array $atts { + * The HTML attributes applied to the menu item's `` element, empty strings are ignored. * - * @type string $title Title attribute. - * @type string $target Target attribute. - * @type string $rel The rel attribute. - * @type string $href The href attribute. + * @type string $title Title attribute. + * @type string $target Target attribute. + * @type string $rel The rel attribute. + * @type string $href The href attribute. * } - * @param WP_Post $item The current menu item. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param int $depth Depth of menu item. Used for padding. + * + * @param WP_Post $item The current menu item. + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param int $depth Depth of menu item. Used for padding. + * * @since 3.6.0 * @since 4.1.0 The `$depth` parameter was added. - * */ $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth); @@ -516,12 +505,12 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) /** * Filters a menu item's title. * - * @param string $title The menu item's title. - * @param WP_Post $item The current menu item. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @param int $depth Depth of menu item. Used for padding. - * @since 4.4.0 + * @param string $title The menu item's title. + * @param WP_Post $item The current menu item. + * @param stdClass $args An object of wp_nav_menu() arguments. + * @param int $depth Depth of menu item. Used for padding. * + * @since 4.4.0 */ $title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth); @@ -533,17 +522,16 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) /** * Filters a menu item's starting output. - * * The menu item's starting output only includes `$args->before`, the opening ``, * the menu item's title, the closing ``, and `$args->after`. Currently, there is * no filter for modifying the opening and closing `
  • ` for a menu item. * - * @param string $item_output The menu item's starting HTML output. - * @param WP_Post $item Menu item data object. - * @param int $depth Depth of menu item. Used for padding. - * @param stdClass $args An object of wp_nav_menu() arguments. - * @since 3.0.0 + * @param string $item_output The menu item's starting HTML output. + * @param WP_Post $item Menu item data object. + * @param int $depth Depth of menu item. Used for padding. + * @param stdClass $args An object of wp_nav_menu() arguments. * + * @since 3.0.0 */ $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); } @@ -555,143 +543,10 @@ function end_el(&$output, $item, $depth = 0, $args = array()) function start_lvl(&$output, $depth = 0, $args = array()) { - } function end_lvl(&$output, $depth = 0, $args = array()) { - - } - -} -/* - -class Submenu_Walker extends Walker_Nav_Menu -{ - protected function get_direct_ancestors($elements) - { - $currentID = get_the_ID(); - $ancestors = []; - - do { - $ancestor = array_pop(array_filter($elements, function ($item) use ($currentID) { - if ($item->ID == $currentID || $item->object_id == $currentID) { - return true; - } else { - return false; - } - })); - $currentID = $ancestor->post_parent; - array_unshift($ancestors, $ancestor); - } while ($ancestor->menu_item_parent > 0); - - return $ancestors; - } - - protected function get_highest_ancestor($elements) - { - return array_pop($this->get_direct_ancestors($elements)); - } - - protected function exclude_top_level_items($elements) - { - return array_filter($elements, function ($item) { - // Top level items have no parent (post_parent=0) everything else will have a post_parent>0 - // so we use PHP falsiness/truthiness to return - return $item->post_parent; - }); - } - - function walk($elements, $max_depth) - { - //$elements = $this->get_direct_ancestors($elements); - //$elements = $this->exclude_top_level_items($elements); - //$elements = submenu_limit($elements, (object)['submenu' => get_the_title()]); - $elements = my_wp_nav_menu_objects_sub_menu($elements, (object)['sub_menu' => true]); - - return parent::walk($elements, $max_depth); // TODO: Change the autogenerated stub } - - } - -function submenu_limit( $items, $args ) { - - if ( empty( $args->submenu ) ) { - return $items; - } - - $ids = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' ); - print_r($ids); - $parent_id = array_pop( $ids ); - $children = submenu_get_children_ids( $parent_id, $items ); - - foreach ( $items as $key => $item ) { - - if ( ! in_array( $item->ID, $children ) ) { - unset( $items[$key] ); - } - } - - return $items; -} - -function submenu_get_children_ids( $id, $items ) { - - $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' ); - - foreach ( $ids as $id ) { - - $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) ); - } - - return $ids; -} - -function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) { - if ( isset( $args->sub_menu ) ) { - $root_id = 0; - - // find the current menu item - foreach ( $sorted_menu_items as $menu_item ) { - if ( $menu_item->current ) { - // set the root id based on whether the current menu item has a parent or not - $root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID; - break; - } - } - - // find the top level parent - if ( ! isset( $args->direct_parent ) ) { - $prev_root_id = $root_id; - while ( $prev_root_id != 0 ) { - foreach ( $sorted_menu_items as $menu_item ) { - if ( $menu_item->ID == $prev_root_id ) { - $prev_root_id = $menu_item->menu_item_parent; - // don't set the root_id to 0 if we've reached the top of the menu - if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent; - break; - } - } - } - } - $menu_item_parents = array(); - foreach ( $sorted_menu_items as $key => $item ) { - // init menu_item_parents - if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID; - if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) { - // part of sub-tree: keep! - $menu_item_parents[] = $item->ID; - } else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) { - // not part of sub-tree: away with it! - unset( $sorted_menu_items[$key] ); - } - } - - return $sorted_menu_items; - } else { - return $sorted_menu_items; - } -}*/ - From 7980a64d7c12d3803546dd9b2b34ab2ed0945f32 Mon Sep 17 00:00:00 2001 From: Scott Limmer Date: Mon, 13 Jun 2022 14:13:30 +1000 Subject: [PATCH 3/3] cleanup twitter.cron.php --- cron/twitter.cron.php | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/cron/twitter.cron.php b/cron/twitter.cron.php index 2443f83..e6db4b9 100644 --- a/cron/twitter.cron.php +++ b/cron/twitter.cron.php @@ -6,10 +6,7 @@ * Time: 4:37 PM */ -const TWITTER_BEARER_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAAEe9gAAAAAAyXXvW1LhcVRB1fCvIAUzExbDfAA%3DhQCppugfuFBDr0qgGsgK6GTPgVYI8uR2DTs3MITOcMRXVONTlT'; -const TWITTER_USER = 'vwsg_web'; -const TWITTER_API_URL = 'https://api.twitter.com/1.1/'; - +require_once __DIR__ . '/../../../../wp-twitter-config.php'; $ch = curl_init( sprintf( @@ -20,7 +17,6 @@ 'screen_name' => TWITTER_USER, 'count' => 10, 'exclude_replies' => true, -// 'include_rts' => false, 'trim_user' => true, 'tweet_mode' => 'extended' ]) @@ -38,35 +34,4 @@ $response = curl_exec($ch); curl_close($ch); -/*$ids = []; -$response = json_decode($response); - -foreach ($response as $tweet) { - $ids[] = $tweet->id; -} - -$ch = curl_init( - sprintf( - '%s%s?%s', - TWITTER_API_URL, - 'statuses/lookup.json', - http_build_query([ - 'id' => implode(',', $ids) - ]) - ) -); - -curl_setopt_array($ch, [ - CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => [ - sprintf('Authorization: Bearer %s', TWITTER_BEARER_TOKEN) - ] -]); - - -$response = curl_exec($ch); -curl_close($ch);*/ - - -file_put_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.TWITTER_USER.'.json', $response); \ No newline at end of file +file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . TWITTER_USER . '.json', $response); \ No newline at end of file