Skip to content

Commit

Permalink
Started with react
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muhsin committed Mar 2, 2017
1 parent 452b33f commit d41300c
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 26 deletions.
83 changes: 83 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"mocha": true,
"node": true
},
"ecmaFeatures": {
"jsx": true,
"modules": true
},
"plugins": [
"eslint-plugin-react"
],
"rules": {
"brace-style": [ 1, "1tbs" ],
// REST API objects include underscores
"camelcase": 0,
"comma-dangle": 0,
"comma-spacing": 1,
// Allows returning early as undefined
"consistent-return": 0,
"dot-notation": 1,
"eqeqeq": [ 2, "allow-null" ],
"eol-last": 1,
"indent": [ 1, "tab", { "SwitchCase": 1 } ],
"key-spacing": 1,
// Most common is "Emitter", should be improved
"new-cap": 1,
"no-cond-assign": 2,
"no-else-return": 1,
"no-empty": 1,
// Flux stores use switch case fallthrough
"no-fallthrough": 0,
"no-lonely-if": 1,
"no-mixed-requires": 0,
"no-mixed-spaces-and-tabs": 1,
"no-multiple-empty-lines": [ 1, { max: 1 } ],
"no-multi-spaces": 1,
"no-nested-ternary": 1,
"no-new": 1,
"no-process-exit": 1,
"no-shadow": 1,
"no-spaced-func": 1,
"no-trailing-spaces": 1,
"no-undef": 1,
"no-underscore-dangle": 0,
// Allows Chai `expect` expressions
"no-unused-expressions": 0,
"no-unused-vars": 1,
// Teach eslint about React+JSX
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
// Allows function use before declaration
"no-use-before-define": [ 2, "nofunc" ],
// We split external, internal, module variables
"one-var": 0,
"operator-linebreak": [ 1, "after", { "overrides": {
"?": "after",
":": "after"
} } ],
"padded-blocks": [ 1, "never" ],
"quote-props": [ 1, "consistent-as-needed" ],
"quotes": [ 1, "single", "avoid-escape" ],
"semi-spacing": 1,
"keyword-spacing": [ 1 ],
"space-before-blocks": [ 1, "always" ],
"space-before-function-paren": [ 1, "never" ],
// Our array literal index exception violates this rule
"space-in-brackets": 0,
"space-in-parens": [ 1, "always" ],
"space-infix-ops": [ 1, { "int32Hint": false } ],
// Ideal for "!" but not for "++"
"space-unary-ops": 0,
// Assumed by default with Babel
"strict": [ 2, "never" ],
"valid-jsdoc": [ 1, { "requireReturn": false } ],
// Common top-of-file requires, expressions between external, interal
"vars-on-top": 1,
"yoda": 0
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
debug.log
build
75 changes: 49 additions & 26 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
* @package ReactVerse
*/

/**
* Check to ensure latest version
*/
if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
require get_template_directory() . '/inc/compat-warnings.php';
return;
}

if ( ! defined( 'REACTVERSE_VERSION' ) ) {
define( 'REACTVERSE_VERSION', time() );
}

if ( ! defined( 'REACTVERSE_APP' ) ) {
define( 'REACTVERSE_APP', 'reactverse-app' );
}

if ( ! function_exists( 'reactverse_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
Expand All @@ -20,6 +32,13 @@
* as indicating support for post thumbnails.
*/
function reactverse_setup() {
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on ReactVerse, use a find and replace
* to change 'ReactVerse' to the name of your theme in all the template files.
*/
load_theme_textdomain( 'reactverse', get_template_directory() . '/languages' );

// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
Expand Down Expand Up @@ -56,18 +75,6 @@ function reactverse_setup() {
'caption',
) );

/*
* Enable support for Post Formats.
* See https://developer.wordpress.org/themes/functionality/post-formats/
*/
add_theme_support( 'post-formats', array(
'aside',
'image',
'video',
'quote',
'link',
) );

add_post_type_support( 'post', 'comments' );
add_post_type_support( 'page', 'comments' );
}
Expand All @@ -79,23 +86,39 @@ function reactverse_setup() {
*/
function reactverse_scripts() {

wp_enqueue_style( 'reactverse-style', get_stylesheet_uri() );

wp_enqueue_style( 'reactverse-style', get_template_directory_uri() . '/build/style.css', array(), REACTVERSE_VERSION );
wp_enqueue_script( REACTVERSE_APP, get_template_directory_uri() . '/build/app.js', array( 'jquery' ), REACTVERSE_VERSION, true );
if ( is_child_theme() ) {
wp_enqueue_style( 'reactverse-child-style', get_stylesheet_uri() );
}

$url = trailingslashit( home_url() );
$path = trailingslashit( parse_url( $url, PHP_URL_PATH ) );

wp_scripts()->add_data( 'reactverse-react', 'data', sprintf( 'var ReactVerseSettings = %s;', wp_json_encode( array(
'nonce' => wp_create_nonce( 'wp_rest' ),
// 'localStorageCache' => ! is_customize_preview(),
'user' => get_current_user_id(),
'title' => get_bloginfo( 'name', 'display' ),
'path' => $path,
'URL' => array(
'api' => esc_url_raw( get_rest_url( null, '/wp/v2' ) ),
'menuApi' => esc_url_raw( get_rest_url( null, '/wp-api-menus/v2' ) ),
'root' => esc_url_raw( $url ),
),
) ) ) );
$reactverse_settings = sprintf(
'var SiteSettings = %s; var ReactVerseSettings = %s;',
wp_json_encode( array(
'endpoint' => esc_url_raw( $url ),
'nonce' => wp_create_nonce( 'wp_rest' ),
) ),
wp_json_encode( array(
'user' => get_current_user_id(),
'userDisplay' => $user ? $user->display_name : '',
'frontPage' => array(
'page' => $front_page_slug,
'blog' => $blog_page_slug,
),
'URL' => array(
'base' => esc_url_raw( $url ),
'path' => $path,
),
'meta' => array(
'title' => get_bloginfo( 'name', 'display' ),
'description' => get_bloginfo( 'description', 'display' ),
),
) )
);
wp_add_inline_script( REACTVERSE_APP, $reactverse_settings, 'before' );
}
add_action( 'wp_enqueue_scripts', 'reactverse_scripts' );

Expand Down
53 changes: 53 additions & 0 deletions inc/compat-warnings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* ReactVerse compatbility warnings
*
* Prevents ReactVerse from running on WordPress versions without the REST API,
* since this theme requires API functionality. If this file is loaded,
* we know we have an incompatible WordPress.
*
* @package ReactVerse
*/

/**
* Prevent switching to ReactVerse on old versions of WordPress.
*
* Switches to the default theme.
*/
function reactverse_switch_theme() {
switch_theme( WP_DEFAULT_THEME );
unset( $_GET['activated'] );
add_action( 'admin_notices', 'reactverse_upgrade_notice' );
}
add_action( 'after_switch_theme', 'reactverse_switch_theme' );

/**
* Adds a message for unsuccessful theme switch.
*
* Prints an update nag after an unsuccessful attempt to switch to
* ReactVerse on WordPress versions prior to 4.7.
*/
function reactverse_upgrade_notice() {
$message = __( 'ReactVerse requires WordPress 4.7 or higher. Please update your site and try again.', 'ReactVerse' );
printf( '<div class="error"><p>%s</p></div>', $message ); /* WPCS: xss ok. */
}

/**
* Prevents the Customizer from being loaded on WordPress versions prior to 4.7.
*/
function reactverse_customize() {
wp_die( __( 'ReactVerse requires WordPress 4.7 or higher. Please update your site and try again.', 'reactverse' ), '', array(
'back_link' => true,
) );
}
add_action( 'load-customize.php', 'reactverse_customize' );

/**
* Prevents the Theme Preview from being loaded on WordPress versions prior to 4.7.
*/
function reactverse_preview() {
if ( isset( $_GET['preview'] ) ) {
wp_die( __( 'ReactVerse requires WordPress 4.7 or higher. Please update your site and try again.', 'reactverse' ) );
}
}
add_action( 'template_redirect', 'reactverse_preview' );
31 changes: 31 additions & 0 deletions js/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Load in the babel (es6) polyfill
import 'babel-polyfill';

import React from 'react';
import { render } from 'react-dom';

// Load the CSS
require( '../sass/style.scss' );

function renderApp() {
render(
(
<Hello />
),
document.getElementById( 'main' )
);
}

class Hello extends React.Component {
render() {
return (
<div className="placeholder">
<p>This content is loaded through React.</p>
</div>
);
}
}

document.addEventListener( 'DOMContentLoaded', function() {
renderApp();
} );
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "react-verse",
"version": "1.0.0",
"description": "A React-based WordPress theme",
"repository": {
"type": "git",
"url": "git+https://github.com/m-muhsin/react-verse.git"
},
"keywords": [
"react",
"restapi",
"wordpress"
],
"author": "Muhammad Muhsin",
"license": "ISC",
"scripts": {
"test": "NODE_ENV=test NODE_PATH=test:components TEST_ROOT=js test/runner.js",
"build": "NODE_ENV=production webpack",
"dev": "webpack",
"watch": "webpack --watch",
"lint": "eslint js --ext=js,jsx",
"zip": "node zip-theme.js"
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"devDependencies": {
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^6.3.2",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"babel-plugin-webpack-alias": "^2.1.2",
"css-loader": "^0.26.2",
"eslint": "^3.16.1",
"eslint-loader": "^1.6.3",
"eslint-plugin-react": "^6.10.0",
"extract-text-webpack-plugin": "^1.0.1",
"lodash-webpack-plugin": "^0.11.0",
"node-sass": "^3.13.1",
"sass-loader": "^4.1.0",
"webpack": "^1.13.3",
"style-loader": "^0.13.2"
}
}
35 changes: 35 additions & 0 deletions sass/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
Theme Name: React Verse
Theme URI: http://underscores.me/
Author: Muhammad Muhsin
Author URI: http://themes.redradar.net
Description: A react-based theme.
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: reactverse
Tags: react, wp-api
*/

body {
background-color: #01304b;
}

.placeholder {
text-align: center;
margin-top: 40vh;
}

.placeholder p {
color: #91f2e5;
font-family: sans-serif;
font-size: 2em;
}

.screen-reader-text{
clip:rect(1px, 1px, 1px, 1px);
position:absolute !important;
height:1px;
width:1px;
overflow:hidden
}
Loading

0 comments on commit d41300c

Please sign in to comment.