Skip to content

Commit 7b9aeee

Browse files
committed
Merge pull request #35 from junichi11/child-theme
Add action for child theme
2 parents 0f4474f + a5b05c9 commit 7b9aeee

12 files changed

Lines changed: 1355 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This is NetBeans plugin for WordPress.
2626
- run command (wp-cli)
2727
- upgrade notification
2828
- create a .htaccess file for permalink
29+
- create a new child theme action (create a style.css for child theme)
2930

3031
### Impotant Files
3132
It contains wp-config.php
@@ -83,6 +84,12 @@ WordPress version number is also displayed.
8384
### Create New Theme Action
8485
Right-click Project > WordPress > Create Theme
8586

87+
### Create New Child Theme Action
88+
Right-click Project > WordPress > Create Child Theme
89+
90+
- Create a new directory for child theme
91+
- Add style.css for child theme
92+
8693
#### Underscores
8794
Create theme from [Underscores | A Starter Theme for WordPress](http://underscores.me/). Underscores is awesome!
8895
This plugin uses [Automattic/_s · GitHub](https://github.com/automattic/_s).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OpenIDE-Module-Display-Category=PHP
22
OpenIDE-Module-Long-Description=\
33
Support for WordPress.This plugin provides the following features.\n\
4-
<ul>\n<li>New Wordpress Project wizard</li>\n<li>Create theme(Underscores, Barebones) action</li>\n<li>Create plugin action</li>\n<li>Hyperlink navigation and code completion for filter and action function</li>\n<li>Shortcut nodes for theme and plugin directory</li>\n<li>Zip compress for your custom theme and plugin directory</li>\n<li>wp-cli support</li>\n</ul>
4+
<ul>\n<li>New Wordpress Project wizard</li>\n<li>Create theme(Underscores, Barebones) action</li>\n<li>Create child theme action</li>\n<li>Create plugin action</li>\n<li>Hyperlink navigation and code completion for filter and action function</li>\n<li>Shortcut nodes for theme and plugin directory</li>\n<li>Zip compress for your custom theme and plugin directory</li>\n<li>wp-cli support</li>\n</ul>
55
OpenIDE-Module-Name=PHP WordPress Blog/CMS
66
OpenIDE-Module-Short-Description=Support for WordPress

src/org/netbeans/modules/php/wordpress/WordPressActionsExtender.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.netbeans.modules.php.spi.framework.PhpModuleActionsExtender;
4949
import org.netbeans.modules.php.spi.framework.actions.RunCommandAction;
5050
import org.netbeans.modules.php.wordpress.commands.WordPressCli;
51+
import org.netbeans.modules.php.wordpress.ui.actions.CreateChildThemeAction;
5152
import org.netbeans.modules.php.wordpress.ui.actions.CreatePermalinkHtaccessAction;
5253
import org.netbeans.modules.php.wordpress.ui.actions.CreatePluginAction;
5354
import org.netbeans.modules.php.wordpress.ui.actions.CreateThemeAction;
@@ -86,6 +87,7 @@ public RunCommandAction getRunCommandAction() {
8687
public List<? extends Action> getActions() {
8788
List<Action> actions = new ArrayList<Action>();
8889
actions.add(CreateThemeAction.getInstance());
90+
actions.add(CreateChildThemeAction.getInstance());
8991
actions.add(CreatePluginAction.getInstance());
9092
actions.add(new RefreshCodeCompletionAction());
9193
actions.add(CreatePermalinkHtaccessAction.getInstance());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
</head>
7+
<body>
8+
Create a new WordPress Child Theme style file (style.css).
9+
</body>
10+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Theme Name: <#if name?? && name != "">${name}</#if>
3+
Theme URI: <#if uri?? && uri != "">${uri}</#if>
4+
Description: <#if description?? && description != "">${description}</#if>
5+
Version: <#if version?? && version != "">${version}</#if>
6+
Author: <#if author?? && author != "">${author}</#if>
7+
Author URI: <#if authorUri?? && authorUri != "">${authorUri}</#if>
8+
Template: <#if parent?? && parent != "">${parent}</#if>
9+
License: GNU General Public License v2 or later
10+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
11+
Tags: <#if tags?? && tags != "">${tags}</#if>
12+
Text Domain: <#if textDomain?? && textDomain != "">${textDomain}</#if>
13+
*/
14+
15+
@import url("../<#if parent?? && parent != "">${parent}<#else>parent</#if>/style.css");

src/org/netbeans/modules/php/wordpress/resources/templates/package-info.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
content = "style.css",
2828
description = "WpThemeStyleDescription.html",
2929
scriptEngine = "freemarker"),
30+
@TemplateRegistration(
31+
folder = "WordPress",
32+
iconBase = WordPress.WP_ICON_16,
33+
displayName = "#WordPress_Child_Theme_Style_DisplayName",
34+
content = "child-style.css",
35+
description = "WpChildThemeStyleDescription.html",
36+
scriptEngine = "freemarker"),
3037
@TemplateRegistration(
3138
folder = "WordPress",
3239
// iconBase = WordPress.WP_ICON_16,
@@ -39,6 +46,7 @@
3946
"WordPress_Plugin_Template_DisplayName=WordPress Plugin File",
4047
"WordPress_Plugin_Readme_DisplayName=WordPress Plugin Readme File",
4148
"WordPress_Theme_Style_DisplayName=WordPress Theme Style File",
49+
"WordPress_Child_Theme_Style_DisplayName=WordPress Child Theme Style File",
4250
"WordPress_Permalink_Htaccess_DisplayName=WordPress .htaccess file for permalink"})
4351
package org.netbeans.modules.php.wordpress.resources.templates;
4452

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7+
* Other names may be trademarks of their respective owners.
8+
*
9+
* The contents of this file are subject to the terms of either the GNU
10+
* General Public License Version 2 only ("GPL") or the Common
11+
* Development and Distribution License("CDDL") (collectively, the
12+
* "License"). You may not use this file except in compliance with the
13+
* License. You can obtain a copy of the License at
14+
* http://www.netbeans.org/cddl-gplv2.html
15+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16+
* specific language governing permissions and limitations under the
17+
* License. When distributing the software, include this License Header
18+
* Notice in each file and include the License file at
19+
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20+
* particular file as subject to the "Classpath" exception as provided
21+
* by Oracle in the GPL Version 2 section of the License file that
22+
* accompanied this code. If applicable, add the following below the
23+
* License Header, with the fields enclosed by brackets [] replaced by
24+
* your own identifying information:
25+
* "Portions Copyrighted [year] [name of copyright owner]"
26+
*
27+
* If you wish your version of this file to be governed by only the CDDL
28+
* or only the GPL Version 2, indicate your decision by adding
29+
* "[Contributor] elects to include this software in this distribution
30+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
31+
* single choice of license, a recipient has the option to distribute
32+
* your version of this file under either the CDDL, the GPL Version 2 or
33+
* to extend the choice of license to its licensees as provided above.
34+
* However, if you add GPL Version 2 code and therefore, elected the GPL
35+
* Version 2 license, then the option applies only if the new code is
36+
* made subject to such option by the copyright holder.
37+
*
38+
* Contributor(s):
39+
*
40+
* Portions Copyrighted 2013 Sun Microsystems, Inc.
41+
*/
42+
package org.netbeans.modules.php.wordpress.ui.actions;
43+
44+
import java.io.IOException;
45+
import java.util.ArrayList;
46+
import java.util.Collections;
47+
import java.util.HashMap;
48+
import java.util.List;
49+
import java.util.Map;
50+
import java.util.logging.Level;
51+
import java.util.logging.Logger;
52+
import javax.swing.SwingUtilities;
53+
import javax.swing.event.ChangeEvent;
54+
import javax.swing.event.ChangeListener;
55+
import org.netbeans.modules.csl.api.UiUtils;
56+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
57+
import org.netbeans.modules.php.api.validation.ValidationResult;
58+
import org.netbeans.modules.php.spi.framework.actions.BaseAction;
59+
import org.netbeans.modules.php.wordpress.modules.WordPressModule;
60+
import org.netbeans.modules.php.wordpress.ui.wizards.CreateChildThemePanel;
61+
import org.netbeans.modules.php.wordpress.util.WPUtils;
62+
import org.netbeans.modules.php.wordpress.validators.WordPressDirectoryNameValidator;
63+
import org.openide.DialogDescriptor;
64+
import org.openide.DialogDisplayer;
65+
import org.openide.filesystems.FileObject;
66+
import org.openide.filesystems.FileUtil;
67+
import org.openide.loaders.DataFolder;
68+
import org.openide.loaders.DataObject;
69+
import org.openide.loaders.DataObjectNotFoundException;
70+
import org.openide.util.Exceptions;
71+
import org.openide.util.NbBundle;
72+
73+
/**
74+
*
75+
* @author junichi11
76+
*/
77+
public final class CreateChildThemeAction extends BaseAction implements ChangeListener {
78+
79+
private CreateChildThemePanel panel;
80+
private DialogDescriptor descriptor;
81+
private final List<String> themes = new ArrayList<String>();
82+
private static final CreateChildThemeAction INSTANCE = new CreateChildThemeAction();
83+
private static final Logger LOGGER = Logger.getLogger(CreateChildThemeAction.class.getName());
84+
private static final long serialVersionUID = -2231810352652363996L;
85+
86+
private CreateChildThemeAction() {
87+
}
88+
89+
public static CreateChildThemeAction getInstance() {
90+
return INSTANCE;
91+
}
92+
93+
@Override
94+
protected String getFullName() {
95+
return getPureName();
96+
}
97+
98+
@Override
99+
@NbBundle.Messages("LBL_CreateChildThemeBaseAction=Create Child Theme")
100+
protected String getPureName() {
101+
return Bundle.LBL_CreateChildThemeBaseAction();
102+
}
103+
104+
@Override
105+
@NbBundle.Messages({
106+
"CreateChildThemeAction.notFound.template=Not found : style.css template file for child theme",
107+
"CreateChildThemeAction.childFolder.error=Can't create a child theme folder"
108+
})
109+
protected void actionPerformed(PhpModule phpModule) {
110+
if (!WPUtils.isWP(phpModule)) {
111+
// called via shortcut
112+
return;
113+
}
114+
themes.clear();
115+
116+
// get themes directory
117+
WordPressModule wpModule = WordPressModule.Factory.forPhpModule(phpModule);
118+
if (wpModule == null) {
119+
return;
120+
}
121+
final FileObject themesDirectory = wpModule.getThemesDirectory();
122+
if (themesDirectory == null) {
123+
return;
124+
}
125+
for (FileObject child : themesDirectory.getChildren()) {
126+
if (child.isFolder()) {
127+
themes.add(child.getNameExt());
128+
}
129+
}
130+
131+
if (themes.isEmpty()) {
132+
return;
133+
}
134+
135+
Collections.sort(themes);
136+
137+
// show dialog
138+
SwingUtilities.invokeLater(new Runnable() {
139+
140+
@Override
141+
public void run() {
142+
getDescriptor().setValid(false);
143+
DialogDisplayer displayer = DialogDisplayer.getDefault();
144+
if (displayer.notify(getDescriptor()) == DialogDescriptor.OK_OPTION) {
145+
try {
146+
createChildTheme(themesDirectory);
147+
} catch (IOException ex) {
148+
Exceptions.printStackTrace(ex);
149+
}
150+
}
151+
clear();
152+
}
153+
});
154+
}
155+
156+
private void createChildTheme(FileObject themesDirectory) throws IOException, DataObjectNotFoundException {
157+
// get template
158+
FileObject template = FileUtil.getConfigFile("Templates/WordPress/child-style.css"); // NOI18N
159+
if (template == null) {
160+
LOGGER.log(Level.WARNING, Bundle.CreateChildThemeAction_notFound_template());
161+
return;
162+
}
163+
// create child theme directory and style.css
164+
FileObject childThemeDirectory = themesDirectory.createFolder(getPanel().getChildDirectoryName());
165+
if (childThemeDirectory == null) {
166+
LOGGER.log(Level.WARNING, Bundle.CreateChildThemeAction_childFolder_error());
167+
return;
168+
}
169+
DataObject templateDataObject = DataObject.find(template);
170+
DataFolder targetFolder = DataFolder.findFolder(childThemeDirectory);
171+
Map<String, String> parameters = new HashMap<String, String>();
172+
parameters.put("name", getPanel().getChildThemeName()); // NOI18N
173+
parameters.put("parent", getPanel().getParentThemeName()); // NOI18N
174+
parameters.put("uri", getPanel().getChildThemeUri()); // NOI18N
175+
parameters.put("description", getPanel().getDescription()); // NOI18N
176+
parameters.put("version", getPanel().getVersion()); // NOI18N
177+
parameters.put("author", getPanel().getAuthor()); // NOI18N
178+
parameters.put("authorUri", getPanel().getAuthorUri()); // NOI18N
179+
parameters.put("tags", getPanel().getTags()); // NOI18N
180+
parameters.put("textDomain", getPanel().getTextDomain()); // NOI18N
181+
DataObject styleCssDataObject = templateDataObject.createFromTemplate(targetFolder, "style.css", parameters); // NOI18N
182+
if (styleCssDataObject != null) {
183+
UiUtils.open(styleCssDataObject.getPrimaryFile(), 0);
184+
}
185+
}
186+
187+
private void clear() {
188+
if (panel != null) {
189+
panel.removeChangeListener(this);
190+
}
191+
panel = null;
192+
descriptor = null;
193+
themes.clear();
194+
}
195+
196+
@Override
197+
public void stateChanged(ChangeEvent e) {
198+
if (descriptor == null || panel == null) {
199+
return;
200+
}
201+
String childThemeName = getPanel().getChildDirectoryName();
202+
WordPressDirectoryNameValidator validator = new WordPressDirectoryNameValidator();
203+
ValidationResult result = validator.validateName(childThemeName)
204+
.validateExistingName(childThemeName, themes)
205+
.getResult();
206+
207+
// errors
208+
List<ValidationResult.Message> errors = result.getErrors();
209+
if (!errors.isEmpty()) {
210+
getDescriptor().setValid(false);
211+
getPanel().setError(result.getErrors().get(0).getMessage());
212+
return;
213+
}
214+
215+
// warnings
216+
List<ValidationResult.Message> warnings = result.getWarnings();
217+
if (!warnings.isEmpty()) {
218+
getDescriptor().setValid(false);
219+
getPanel().setError(result.getWarnings().get(0).getMessage());
220+
return;
221+
}
222+
223+
// everything ok
224+
getDescriptor().setValid(true);
225+
getPanel().setError(" "); // NOI18N
226+
}
227+
228+
private CreateChildThemePanel getPanel() {
229+
if (panel == null) {
230+
panel = new CreateChildThemePanel(themes);
231+
panel.addChangeListener(this);
232+
}
233+
return panel;
234+
}
235+
236+
private DialogDescriptor getDescriptor() {
237+
if (descriptor == null) {
238+
descriptor = new DialogDescriptor(getPanel(), Bundle.LBL_CreateChildThemeBaseAction(), true, DialogDescriptor.OK_CANCEL_OPTION, null, null);
239+
}
240+
return descriptor;
241+
}
242+
243+
}

src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,23 @@ CreateThemePanel.themeNameLabel.text=Theme Name:
3535
CreateThemePanel.themeNameTextField.text=
3636
NewProjectConfigurationPanel.useWpCliRadioButton.text=Use wp-cli
3737
CreatePermalinkHtaccessPanel.messageLabel.text=MESSAGE
38+
CreateChildThemePanel.parentThemeLabel.text=Parent Theme:
39+
CreateChildThemePanel.errorLabel.text=ERROR
40+
CreateChildThemePanel.childThemeUriLabel.text=Child Theme URI:
41+
CreateChildThemePanel.descriptionLabel.text=Description:
42+
CreateChildThemePanel.versionLabel.text=Version:
43+
CreateChildThemePanel.authorLabel.text=Author:
44+
CreateChildThemePanel.tagsLabel.text=Tags:
45+
CreateChildThemePanel.textDomainLabel.text=Text Domain:
46+
CreateChildThemePanel.childThemeUriTextField.text=
47+
CreateChildThemePanel.descriptionTextField.text=
48+
CreateChildThemePanel.versionTextField.text=1.0.0
49+
CreateChildThemePanel.authorTextField.text=
50+
CreateChildThemePanel.tagsTextField.text=
51+
CreateChildThemePanel.textDomainTextField.text=
52+
CreateChildThemePanel.authorUriLabel.text=Author URI:
53+
CreateChildThemePanel.authorUriTextField.text=
54+
CreateChildThemePanel.childDirectoryNameLabel.text=Child Directory Name:
55+
CreateChildThemePanel.childDirectoryNameTextField.text=
56+
CreateChildThemePanel.childThemeNameLabel.text=Child Theme Name:
57+
CreateChildThemePanel.childThemeNameTextField.text=

0 commit comments

Comments
 (0)