Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wordpress Core Settings Patch #661

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
317 changes: 317 additions & 0 deletions settings-patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
<?php
/**
* Registers WordPress options not loaded by Settings API by default
*/
function patch_whitelist_options() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method patch_whitelist_options has 286 lines of code (exceeds 25 allowed). Consider refactoring.

$whitelist_options = array(
'discussion' => array(
'default_pingback_flag' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Attempt to notify any blogs linked to from the article', 'wp-graphql' )
),
'comments_notify' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Anyone posts a comment', 'wp-graphql' )
),
'moderation_notify' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'A comment is held for moderation', 'wp-graphql' )
),
'comment_moderation' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Comment must be manually approved', 'wp-graphql' )
),
'require_name_email' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Comment author must fill out name and email', 'wp-graphql' )
),
'comment_whitelist' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Comment author must have a previously approved comment', 'wp-graphql' )
),
'comment_max_links' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'The number of links necessary for a comment to be queued. (For protection against spam comments)', 'wp-graphql' )
),
'moderation_keys' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'When a comment contains any of these words in its content, name, URL, email, or IP address, it will be held in the moderation queue. One word or IP address per line. It will match inside words, so “press” will match “WordPress”.', 'wp-graphql' )
),
'blacklist_keys' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'When a comment contains any of these words in its content, name, URL, email, or IP address, it will be put in the trash. One word or IP address per line. It will match inside words, so “press” will match “WordPress”.', 'wp-graphql' )
),
'show_avatars' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Show Avatars', 'wp-graphql' )
),
'avatar_rating' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'G', 'PG', 'R', 'X' ),
),
),
'type' => 'string',
'description' => __( 'Avatar content rating', 'wp-graphql' )
),
'avatar_default' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array(
'mystery',
'blank',
'gravatar_default',
'identicon',
'wavatar',
'monsterid',
'retro'
),
),
),
'type' => 'string',
'description' => __( 'For users without a custom avatar of their own, you can either display a generic logo or a generated one based on their email address', 'wp-graphql' )
),
'close_comments_for_old_posts' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Automatically close comments on articles older than a number of days', 'wp-graphql' )
),
'close_comments_days_old' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'The number of days required for automatically closing the comments on an article', 'wp-graphql' )
),
'thread_comments' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Enable threaded (nested) comments', 'wp-graphql' ),
'default' => false
),
'thread_comments_depth' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'Maximum level of comment thread depth', 'wp-graphql' )
),
'page_comments' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Break comments into pages', 'wp-graphql' ),
'default' => false
),
'comments_per_page' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'Number of top level comments per page', 'wp-graphql' )
),
'default_comments_page' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'newest', 'oldest' ),
),
),
'type' => 'string',
'description' => __( 'Comment page displayed by default', 'wp-graphql' )
),
'comment_order' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'asc', 'desc' ),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so for fields like this that are exposed as an Enum in REST should probably also be exposed in GraphQL as an enum.

No reason this should be a String in GraphQL if we have the power to validate against a static enum already 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

register_setting doesn't have any enum type to my knowledge. string, integer, and boolean are the only types as far as I know. I used register_initial_setting as an example.

),
),
'type' => 'string',
'description' => __( 'Order comments are displayed in', 'wp-graphql' )
),
'comment_registration' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Users must be registered and logged in to comment', 'wp-graphql' ),
'default' => false
),
'show_comments_cookies_opt_in' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Show comments cookies opt-in checkbox', 'wp-graphql' )
),
),
'media' => array(
'thumbnail_size_w' => array(
'show_in_rest' => array(
'name' => 'thumbnail_size_width'
),
'type' => 'integer',
'description' => __( 'Maximum width for thumbnail-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
'thumbnail_size_h' => array(
'show_in_rest' => array(
'name' => 'thumbnail_size_height'
),
'type' => 'integer',
'description' => __( 'Maximum height for thumbnail-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
'thumbnail_crop' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Crop thumbnail to exact dimensions (normally thumbnails are proportional)', 'wp-graphql' )
),
'medium_size_w' => array(
'show_in_rest' => array(
'name' => 'medium_size_width'
),
'type' => 'integer',
'description' => __( 'Maximum width for medium-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
'medium_size_h' => array(
'show_in_rest' => array(
'name' => 'medium_size_height'
),
'type' => 'integer',
'description' => __( 'Maximum height for medium-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
'large_size_w' => array(
'show_in_rest' => array(
'name' => 'large_size_width'
),
'type' => 'integer',
'description' => __( 'Maximum width for large-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
'large_size_h' => array(
'show_in_rest' => array(
'name' => 'large_size_height'
),
'type' => 'integer',
'description' => __( 'Maximum height for large-sized content in pixels to use when adding an image to the Media Library', 'wp-graphql' )
),
/**
* TODO: Get more info on these three options
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kidunot89 I'll have to look into this as well 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kidunot89 looks like image_default_size should be an Enum of registered image sizes, eh? 🤔

*/
// 'image_default_size' => array(
// 'show_in_rest',
// 'type',
// 'description'
// ),
// 'image_default_align' => array(
// 'show_in_rest',
// 'type',
// 'description'
// ),
// 'image_default_link_type' => array(
// 'show_in_rest',
// 'type',
// 'description'
// ),
'uploads_use_yearmonth_folders' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Organize my uploads into month- and year-based folders', 'wp-graphql' )
)
),
'permalink' => array(
'permalink_structure' => array(
'show_in_rest' => array(
'name' => 'structure'
),
'type' => 'string',
'description' => __( 'Custom URL structures use for posts', 'wp-graphql' )
),
'category_base' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'Base Category URLs', 'wp-graphql' )
),
'tag_base' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'Base Tag URLs', 'wp-graphql' )
),
),
'privacy' => array(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the following php error:

( ! ) Notice: register_setting was called with an argument that is <strong>deprecated</strong> since version 3.5.0! The "privacy" options group has been removed. Use another settings group. in /app/public/wp-includes/functions.php on line 4115

It also looks like the privacy options have their own capabilities associated: https://github.com/WordPress/WordPress/blob/47e29ccba94fe9342785585b80ddc9b4a8039aa2/wp-admin/privacy.php#L12

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't gonna that error, but I have used it in production yet. I'll also look into it when I can

'wp_page_for_privacy_policy' => array(
'show_in_rest' => array(
'name' => 'page'
),
'type' => 'string',
'description' => __( 'WP Page ID of Privacy Policy page', 'wp-graphql' )
),
),
'reading' => array(
'posts_per_rss' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'Number of most recent syndication feeds shown per page', 'wp-graphql' )
),
'rss_use_excerpt' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array( '0', '1' ),
),
),
'type' => 'string',
'description' => __( 'For each article in a feed, show "full text (0)" or "summary (1)"', 'wp-graphql' )
),
'show_on_front' => array(
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'posts', 'page' ),
),
),
'type' => 'string',
'description' => __( 'Your homepage displays', 'wp-graphql' )
),
'page_on_front' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'WP Page ID of homepage', 'wp-graphql' )
),
'page_for_posts' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'WP Page ID of blog page', 'wp-graphql' )
),
'page_for_posts_slug' => array(
'show_in_rest' => true,
'type' => 'string',
'description' => __( 'WP slug of blog page', 'wp-graphql' ),
'default' => get_page_uri( get_option( 'page_for_posts', '' ) )
),
'blog_public' => array(
'show_in_rest' => true,
'type' => 'boolean',
'description' => __( 'Discourage search engines from indexing this site', 'wp-graphql' )
),
),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces must be used for alignment; tabs are not allowed

'writing' => array(
'default_email_category' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'WP ID of default mail category', 'wp-graphql' )
),
'default_link_category' => array(
'show_in_rest' => true,
'type' => 'integer',
'description' => __( 'WP ID of default link category', 'wp-graphql' )
),
),
);

/**
* Loop through settings.
*/
foreach( $whitelist_options as $group_name => $group_settings ) {
foreach( $group_settings as $setting_name => $setting_args ) {
register_setting( $group_name, $setting_name, $setting_args );
}
}

}

add_action( 'graphql_init', 'patch_whitelist_options', 1 );